again on function pointer

E

enzo

hi all,

could anyone explain me why this doesn't compile??

#include<iostream>

using namespace std;
class test;

class messenger
{
public:
messenger(void(test::*p_f)() = 0)
: m_f(p_f)
{
}

void
call()
{
(*this.*m_f)();
}

void
set(void(test::*p_f)())
{
m_f = p_f;
}

void (test::*m_f)();
};

class test
{
public:
test()
: m(0)
{
m.set(&test::f);
}

void f()
{
cout << "Hallo! " << endl;
}

messenger m;
};


int
main()
{
test a;
a.m.call();
return 1;
}

thanks
e
~
 
D

David Harmon

could anyone explain me why this doesn't compile??

No fair asking about code that doesn't compile without
quoting the error message.
class messenger
{
public: ....
....
void (test::*m_f)();

m_f points to a member function of class test.

(*this) is an object of class messenger.

You cannot call a test member function on a messenger object.
 

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

Forum statistics

Threads
474,159
Messages
2,570,886
Members
47,419
Latest member
ArturoBres

Latest Threads

Top