473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

mem_fun_ref

Hello.
If I was to call a method of class T, M for each element in a collection,
I'd do it like this:

vector<int> vec;
....
for_each(vec.begin(), vec.end(), mem_fun_ref(&T::M));

Okay, I can see that mem_fun_REF makes it obvious that I must use a
reference, but why? Is it not possible to pass a function by-reference? As I
see it, T::M is being passed as a pointer here.

void func(int& val); func(12); << reference
void func(int* val); int val = 12; func(&val); << pointer

//Anders
Jul 19 '05 #1
2 3488
Hello.
If I was to call a method of class T, M for each element in a collection,
I'd do it like this:

vector<int> vec;
...
for_each(vec.begin(), vec.end(), mem_fun_ref(&T::M));

Okay, I can see that mem_fun_REF makes it obvious that I must use a
reference, but why? Is it not possible to pass a function by-reference?
No, you can only pass the address. As I see it, T::M is being passed as a pointer here.
The REF, in mem_fun_ref, refers to the argument type( of T::M ), which
will be passed to the function call operator.
void func(int& val); func(12); << reference
void func(int* val); int val = 12; func(&val); << pointer

//Anders

Jul 19 '05 #2
On Mon, 30 Jun 2003 22:34:24 +0200, "Anders" <gr*******@adslhome.dk>
wrote:
Hello.
If I was to call a method of class T, M for each element in a collection,
I'd do it like this:

vector<int> vec;
...
for_each(vec.begin(), vec.end(), mem_fun_ref(&T::M));
That can't be legal - you can't call a T member function on an int
reference.

Okay, I can see that mem_fun_REF makes it obvious that I must use a
reference, but why? Is it not possible to pass a function by-reference? As I
see it, T::M is being passed as a pointer here.

void func(int& val); func(12); << reference
void func(int* val); int val = 12; func(&val); << pointer


The ref refers to the the fact that the object passed as the first
argument of the functor must be a reference. e.g.

T t;

mem_fun_ref(&T::M)(t); //pass T reference

mem_fun(&T::M)(&t); //pass T pointer

so in practice you use mem_fun_ref for containers like:
vector<T>
and mem_fun for containers like:
vector<T*>

Tom
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jason Heyes | last post by:
How do I rewrite the following code so that I am taking advantage of the STL? The purpose of the code is to determine whether a vector contains any objects of MyClass that are "true" (i.e.,...
13
by: Ioannis Vranos | last post by:
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
4
by: dzikus | last post by:
Why the following code does not compile? If I change the function argument to value (not reference) everything is ok. Is it possible to modify such code that it will compile with reference argument...
4
by: ShaneG | last post by:
We have ptr_fun to handle functions, mem_fun to handle member functions that will be called through a pointer, and mem_fun_ref to handle member functions that will be called through a reference. ...
2
by: tibirna | last post by:
Hello mem_fun_ref(&(std::list<int>::size)) is compiled as valid by gcc 3.3.4 and causes an error ("defined-id needed") with gcc 4.0.2. The later requires mem_fun_ref(&std::list<int>::size)...
2
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
12
by: Jim Langston | last post by:
I want to change a class's methods at run time. I'm making a game and want NPC objects to have different methods depending on a game designer. That is, they may decide that this mob should be...
2
by: many_years_after | last post by:
hi, cppers: It is said that member function can be as parameter of STL algorithm. BUT when I pass member function whose parameter is a instance of the class, it dose not work. class Point {...
1
by: puzzlecracker | last post by:
Why do we need mem_fun_ref when we use bind1st or bind2nd functors?? or even without binders as in example below: for_each( v2.begin( ), v2.end( ), mem_fun_ref ( &NumVals::display ) ); cout...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.