Re: A pointer in a class points to an other member function in the same class?
From: John Carson (donaldquixote_at_datafast.net.au)
Date: 03/04/04
- Next message: Leor Zolman: "Re: friends and class templates"
- Previous message: Gianni Mariani: "Re: Makefile question [OFF TOPIC]"
- In reply to: James.D: "A pointer in a class points to an other member function in the same class?"
- Next in thread: John Carson: "Re: A pointer in a class points to an other member function in the same class?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 5 Mar 2004 03:04:42 +1100
"James.D" <ddddh@vip.sina.com> wrote in message
news:c27h0t$d76$1@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)
- Next message: Leor Zolman: "Re: friends and class templates"
- Previous message: Gianni Mariani: "Re: Makefile question [OFF TOPIC]"
- In reply to: James.D: "A pointer in a class points to an other member function in the same class?"
- Next in thread: John Carson: "Re: A pointer in a class points to an other member function in the same class?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|