question on mem_fun( )

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

Consider the following program:

#include <iostream>
#include <string>
#include <list>
#include <algorithm>

using namespace std;

class Test
{
public:
Test(const string &str) : s(str) { }

void print()
{
cout << "from Test::print() : " << s << endl;
return;
}

private:
string s;
};

int main()
{
list<Test *> ls;

Test zero("zero");
Test one("one");
Test two("two");

ls.push_back(&zero);
ls.push_back(&one);
ls.push_back(&two);

for_each(ls.begin( ), ls.end( ), mem_fun(Test::print) );

return 0;
}

When I compile this program, I am getting the following compilation
error(as expected):
'Test::print': function call missing argument list; use '&Test::print'
to create a pointer to member

If I put
mem_fun(&Test:print)
it compiles fine.

Why should we pass the address of a member function here
(&Test::print)?
Doesn't the function name Test::print itself give a pointer ?

For example, a free global function name itself can be passed as
argument to for_each( ).
We don't need to pass the address of a global function(though passing
address of a global function is accepted).

What is the difference between passing member function and a global
function to for_each ?

Kindly explain.

Thanks
V.Subramanian
 
B

Barry

Consider the following program:

#include <iostream>
#include <string>
#include <list>
#include <algorithm>

using namespace std;

class Test
{
public:
Test(const string &str) : s(str) { }

void print()
{
cout << "from Test::print() : " << s << endl;
return;
}

private:
string s;
};

int main()
{
list<Test *> ls;

Test zero("zero");
Test one("one");
Test two("two");

ls.push_back(&zero);
ls.push_back(&one);
ls.push_back(&two);

for_each(ls.begin( ), ls.end( ), mem_fun(Test::print) );

return 0;
}

When I compile this program, I am getting the following compilation
error(as expected):
'Test::print': function call missing argument list; use '&Test::print'
to create a pointer to member

If I put
mem_fun(&Test:print)
it compiles fine.

Why should we pass the address of a member function here
(&Test::print)?
Doesn't the function name Test::print itself give a pointer ?

For example, a free global function name itself can be passed as
argument to for_each( ).
We don't need to pass the address of a global function(though passing
address of a global function is accepted).

What is the difference between passing member function and a global
function to for_each ?

Kindly explain.

5.3.1
3 A pointer to member is only formed when an explicit & is used and its
operand is a qualified-id not enclosed in parentheses.

It's different between a free function and the member function

void f();
&f or f are equivalent, maybe the latter one is for C compatibility, I
guess.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top