mem_fun vs mem_fun_ref [英] mem_fun vs mem_fun_ref

查看:91
本文介绍了mem_fun vs mem_fun_ref的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mem_fun和mem_fun_ref之间的确切区别是什么,因为在

我看过的所有例子中,它们的使用方式完全相同?


-

Ioannis Vranos

http: //www23.brinkster.com/noicys

What is the exact difference between mem_fun and mem_fun_ref, since in
all examples I looked at, they are used in exactly the same way?

--
Ioannis Vranos

http://www23.brinkster.com/noicys

推荐答案

" Ioannis Vranos" < iv*@remove.this.grad.com>写了...
"Ioannis Vranos" <iv*@remove.this.grad.com> wrote...
mem_fun和mem_fun_ref之间的确切区别是什么,因为在我看过的所有示例中,它们的使用方式完全相同?
What is the exact difference between mem_fun and mem_fun_ref, since in all
examples I looked at, they are used in exactly the same way?




他们返回不同类型的对象,mem_fun_t和mem_fun_ref_t,

其成员函数operator()依次采用不同的参数,

前者需要T *,后者需要T&。 RTFM。


V



They return objects of different types, mem_fun_t and mem_fun_ref_t,
whose member functions operator(), in turn, take different arguments,
the former takes T*, the latter takes T&. RTFM.

V


Ioannis Vranos写道:
Ioannis Vranos wrote:
mem_fun和mem_fun之间的确切区别是什么? mem_fun_ref,因为在我看过的所有例子中,它们的使用方式完全相同?
What is the exact difference between mem_fun and mem_fun_ref, since in
all examples I looked at, they are used in exactly the same way?




mem_fun返回一个可以用指针调用的对象到一个

对象; mem_fun_ref返回一个可以使用对象

或对象引用调用的对象。像这样:


#include< functional>

#include< iostream>

使用std :: mem_fun;使用std :: mem_fun_ref;

使用std :: cout;


struct S

{

void f()

{

cout<< 叫做S :: f\ n;

}

};


int main()

{

S s;

S * sp =& s;

mem_fun(& S :: f) (sp);

mem_fun_ref(& S :: f)(s);

返回0;

}


TR1'的mem_fn并不需要这个早期的决定; mem_fn(& S :: f)可以用一个对象,一个对象的引用,一个指向对象的指针,一个指向一个对象的指针,或者一个指向一个对象的智能指针来调用$ b $对象。


-


Pete Becker

Dinkumware,Ltd。( http://www.dinkumware.com


Ioannis Vranos写道:
Ioannis Vranos wrote:
mem_fun和mem_fun_ref之间的确切区别是什么,因为在我看过的所有例子中
,它们的使用方式完全相同?
What is the exact difference between mem_fun and mem_fun_ref, since in all examples I looked at, they are used in exactly the same way?




不同之处在于如何调用成员函数的对象

传递给函子:对于从''mem_fun()''返回的仿函数a
传递
指向对象的指针。对于从

''mem_fun_ref()''返回的仿函数,传递对象的引用。虽然

版本都可以折叠成一个函数重载函数

调用运算符,这可能会掩盖某些问题。

-

< mailto:di *********** @ yahoo.com> < http://www.dietmar-kuehl.de/>

< http://www.contendix.com> - 软件开发&咨询



The difference is how the object the member function is called on
is passed to the functor: for a functor returned from ''mem_fun()'' a
pointer to the object is passed. For a functor returned from
''mem_fun_ref()'' a reference to the object is passed. Although both
version could be folded into just one functor overloading the function
call operator, this could obscure certain problems.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting


这篇关于mem_fun vs mem_fun_ref的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆