CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Get the address of a member function

  1. #1
    Join Date
    Mar 2002
    Location
    Portsmouth, UK
    Posts
    121

    Get the address of a member function

    Hi

    I have a static structure with a member int (*repfn)(int state, const char * reply,int tout) – a function pointer.

    I want to initialise the pointer within my dialog constructor to point to a member function of my dialog class.
    Like this:
    Code:
     CEnterDiagnostics::CEnterDiagnostics() : CPropertyPage(CEnterDiagnostics::IDD)
    {
    	//{{AFX_DATA_INIT(CEnterDiagnostics)
    	//}}AFX_DATA_INIT
    
    	StateMachine[0].repfn = OpenReply;
    }
    Where OpenReply is a member of my class CEnterDiagnostics and declared as

    int OpenReply(int state, const char *pReply,int tout);

    But it doesn’t work I get a compiler error

    C2440: '=' : cannot convert from 'int (__thiscall CEnterDiagnostics::*)(int,const char *,int)' to 'int (__cdecl *)(int,const char *,int)'

    Is there a way to do this?

    Thanks

    Alec

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    If it's static, try &OpenReply.

    Jeff

  3. #3
    Join Date
    Mar 2002
    Location
    Portsmouth, UK
    Posts
    121
    OpenReply was not static, I tried making it static and it compiles OK.

    Can you only get a pointer to a static member function?

    Thanks for the help.

  4. #4
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    For function pointers, I use boost::function which is freely available. You can find it at http://www.boost.org/, along with a lot of other template classes.

    Code:
    // define functor
    boost::function<int, CEnterDiagnostics*, int, const char*, int> repfn;
    ...
    
    // assign functor
    repfn = &OpenReply;
    ....
    
    // call functor
    repfn(enterDiagnosticsPtr, state, reply, tout);
    Used in conjunction with boost::bind, this is very powerful.

    Jeff

  5. #5
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Originally posted by AlecJames
    OpenReply was not static, I tried making it static and it compiles OK.

    Can you only get a pointer to a static member function?

    Thanks for the help.
    It's obvious that it won't work like you tried it with a non-static member function. These always recieve as a minimum the pointer to the instance of the object as a parameter, so you'd have to add that to your function declaration somehow.

    But then again, this might not make sense. How does the code which calls repfn know which instance of CEnterDiagnostics to refer to ?

    I'm really wondering how boost achieves this...

    [edit : ups, I just realized that the answer to this question was already provided by Jeff
    repfn(enterDiagnosticsPtr, state, reply, tout);
    So the first parameter is the pointer to the instance of CEnterDiagnostics ]
    Last edited by Yves M; September 4th, 2002 at 10:15 AM.

  6. #6
    Join Date
    Nov 1999
    Location
    Dresden / Germoney
    Posts
    1,402

    noboost - just C++ (in case you#re looking for...)

    To declare a pointer to a member function:

    struct cfoo {
    int bar(int x);
    };

    int (cfoo::*pFooBar)(int) = cfoo::bar;

    // to call, you need an actual instance -
    foo.*pFooBar(17);
    pFoo->*pFooBar(42);


    of course, functors are much more expressive and powerful... ;-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)