473,698 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.be gin(), 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 3502
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.be gin(), 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*******@adsl home.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.b egin(), 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
1439
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., MyClass::is_true() returns true). bool found = false; vector<MyClass>::size_type i; for (i=0; i < objects.size(); i++) { MyClass object = objects; if (object.is_true())
13
5917
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
4900
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 type? Best regards Dominik #include <string> #include <list> #include <algorithm>
4
3110
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. First, why do we need to have seperate mem_fun/mem_fun_ref? The first returns a mem_fun_t, and the second a mem_fun_ref_t. But the only difference between mem_fun_t and mem_fun_ref_t is that they have different operator() methods. But since...
2
1393
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) Is there a some subtle issue that the compiler has to comply with when
2
2514
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 below). Or, rather, it works fine as long as my member functor is const. The problem comes when I wish to use it for a *non*-const functor (see listing 2 below): *** Start listing 1 *************************************************** // test1.cpp
12
1893
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 able to fly for movement, be agressivie for combat, etc... Rather than a bunch of switch statements or if..else I was thinking it would be good to be able to plug in functions/methods to the class. Well, there's the old C way, which I've done...
2
2430
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 { public: int x; int y;
1
2836
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 << endl;
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9161
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7732
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6522
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.