By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,143 Members | 1,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,143 IT Pros & Developers. It's quick & easy.

question on mem_fun( )

P: n/a
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

Sep 8 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
su**************@yahoo.com, India wrote:
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.

--
Thanks
Barry
Sep 8 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.