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


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)


Relevant Pages

  • Re: Why pointer to member function?
    ... > fixed for any type of closure. ... to store both the object reference and the member function pointer. ... look at the wrapper function again. ...
    (comp.lang.cpp)
  • Re: Set up a member function pointer to another classs public functio
    ... Dan wrote: ... I have arrived at the question of how to set up a member function pointer ... BOOL; ... This is a function pointer to a member function of the class CKnockout ...
    (microsoft.public.vc.language)
  • error C2276; passing &member function to _beginthreadex
    ... member function as "static" but I've not been able to find a compile-able ... syntax by going down that road. ... i.e. a reference to the class object itself, and that therefore when passing ... Apologies if I've missed out any code here - suffice to say it all compiles ...
    (microsoft.public.vc.language)
  • Re: error C2276; passing &member function to _beginthreadex
    ... to declare the member function as "static" but I've not been able to find ... a compile-able syntax by going down that road. ... absolute incompatibility between nonstatic functions and _beginthreadex. ... static thread function use it to call a nonstatic member function. ...
    (microsoft.public.vc.language)
  • Re: Need Help with Pointer to Function
    ... If we want to know how to get a function pointer to member function, ... We can download Free Electronic Book from the following web ... Microsoft Community Support ...
    (microsoft.public.vc.mfc)