function pointer

E

enzo

could anyone explain me why this compiles but doesn't work?

void foo()
{
cout << "Hallo" << endl;
}

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

void
call()
{
*m_f;
}
private:
void
(*m_f)();
};

int main()
{
test a( &foo );
a.call();
return -1;
}

thanks
 
P

Peter van Merkerk

enzo said:
could anyone explain me why this compiles but doesn't work?

void foo()
{
cout << "Hallo" << endl;
}

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

void
call()
{
*m_f;

You forgot the brackets; call the function like this:

m_f();

....and your code will work.
 

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,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top