A pointer in a class points to an other member function in the same class?

J

James.D

Hi, I met such a problem:

//---------------------
// .h
class CA
{
protected:
void (CA::*)m_pfn();

public:
CA();
void foo();

static void proc(CA *pObj); // NOTES it is a static member
};

//---------------------
// .cpp
CA::CA()
{
m_pfn = foo;
}

void CA::foo()
{
return;
}

void CA::proc(CA *pObj)
{
if (pObj->m_pfn)
(pObj->*m_pfn)(); // here I got two compile errors
}


I compiled my project in VS.net 2003, and in the line I marked, I got 2
errors: error C2597 and C2568
I don't know how to resolve this problem.

Any suggestions will be appreciated.

Thanks!


James
 
J

John Carson

James.D said:
Hi, I met such a problem:

//---------------------
// .h
class CA
{
protected:
void (CA::*)m_pfn();

Wrong syntax. It should be:

void (CA::*m_pfn)();
public:
CA();
void foo();

static void proc(CA *pObj); // NOTES it is a static member
};

//---------------------
// .cpp
CA::CA()
{
m_pfn = foo;
}

void CA::foo()
{
return;
}

void CA::proc(CA *pObj)
{
if (pObj->m_pfn)
(pObj->*m_pfn)(); // here I got two compile errors
}

Also wrong syntax. It should be

(pObj->*pObj->m_pfn)();

This syntax performs two functions.

1. The first thing you need to do is retrieve the function pointer. The
pObj->m_pfn on the RIGHT returns the pointer value. Call it function_ptr for
short, so we end up with

(pObj->*function_ptr)();

2. The second thing to do is to actually call the member function using the
function pointer. The remainder of the expression does just that. As with
all (non-static) member functions, you need to call the function by
binding it to an object.

To summarise, the object that pObj points to is needed twice: once as the
object storing the function pointer value, and once as the object that you
use to call the function (in this second capacity the object supplies a
"this" pointer so that the member function can access other members if
needed).
 
J

John Carson

I see that I have just wasted my time since you posted this question in two
other newsgroups and received an answer there. It is precisely for this
reason that multi-posting such as this is regarded as a breach of newsgroup
etiquette.
 
R

Rolf Magnus

James.D said:
Hi, I met such a problem:

//---------------------
// .h
class CA
{
protected:
void (CA::*)m_pfn();

public:
CA();
void foo();

static void proc(CA *pObj); // NOTES it is a static member
};

//---------------------
// .cpp
CA::CA()
{
m_pfn = foo;
}

void CA::foo()
{
return;
}

void CA::proc(CA *pObj)
{
if (pObj->m_pfn)
(pObj->*m_pfn)(); // here I got two compile errors
}


I compiled my project in VS.net 2003, and in the line I marked, I got
2 errors: error C2597 and C2568
I don't know how to resolve this problem.

And I don't know what error "C2597" and "C2568" are. Could you please
add the text of those messages? Not everyone has a Microsoft compiler
installed.
 
D

David Harmon

(pObj->*m_pfn)(); // here I got two compile errors
I compiled my project in VS.net 2003, and in the line I marked, I got 2
errors: error C2597 and C2568

There should be some explanatory text associated with the error reports.
Read the text and consider what it says. It's near impossible to
determine the nature of a compile error without reading the message.
 
J

James.D

I am so sorry about that.

Because the comiler I used is a Chinese version, so the text associated with
the error number is in Chinese.
I can't translate it into English prefectly.

:(


James
 
J

James.D

Sorry, I it is almost the first time I asking in newsgroup.

I really don't know the etiqette.
And I will notice this in the next time.

Thank you very much for your answer and your advice.


James
 

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,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top