"James.D" <dd***@vip.sina.com> wrote in message
news:c2**********@mail.cn99.com
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).
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)