How can I use a function pointer to non-static member function of a class.

Discussion in 'C++' started by Assertor, Mar 9, 2006.

  1. Assertor

    Assertor Guest

    Hi All,

    Except function pointer to a static member funciton of a class (or a
    function).

    As the following code, I could pointer a non-static member function of
    a class.
    And "Equal" was printed, the pointer has meaning.

    But how can I use it??
    I.e., I'd like to call the member method by using the function pointer
    (like using c function pointer).


    class TMyClass
    {
    public:
    int DoIt(float a, char b, char c){ ... };
    }

    int (TMyClass::*pt2Member)(float, char, char) = NULL;
    pt2Member = &TMyClass::DoIt;

    // C++
    if(pt2Member== &TMyClass::DoIt)
    cout << "Equal" << endl;

    pt2Member(1.2f, 'a', 'b'); //<------ error.

    As u know, in case that the function pointer piont a non-static member
    function, the pointer can invoke the member function.
    Assertor, Mar 9, 2006
    #1
    1. Advertising

  2. Re: How can I use a function pointer to non-static member functionof a class.

    Assertor wrote:
    > [..]
    > As the following code, I could pointer a non-static member function of
    > a class.
    > And "Equal" was printed, the pointer has meaning.
    >
    > But how can I use it??


    You need an instance of the class in the form of an object, a reference,
    or a pointer.

    > I.e., I'd like to call the member method by using the function pointer
    > (like using c function pointer).
    >
    >
    > class TMyClass
    > {
    > public:
    > int DoIt(float a, char b, char c){ ... };
    > }

    ;

    >
    > int (TMyClass::*pt2Member)(float, char, char) = NULL;
    > pt2Member = &TMyClass::DoIt;


    I suppose this is inside a function of some kind.

    >
    > // C++
    > if(pt2Member== &TMyClass::DoIt)
    > cout << "Equal" << endl;
    >
    > pt2Member(1.2f, 'a', 'b'); //<------ error.


    You need an instance of 'TMyClass', and then use it:

    TMyClass t;

    (t.*pt2Member)(1.2f, 'a', 'b');

    > As u know, in case that the function pointer piont a non-static member
    > function, the pointer can invoke the member function.


    Huh?

    V
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Mar 9, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. slide_o_mix
    Replies:
    0
    Views:
    422
    slide_o_mix
    Oct 15, 2003
  2. Alex
    Replies:
    0
    Views:
    393
  3. Lars Munch
    Replies:
    2
    Views:
    480
    Lars Munch
    Feb 18, 2004
  4. Fraser Ross
    Replies:
    4
    Views:
    1,043
    Fraser Ross
    Aug 14, 2004
  5. dolphin
    Replies:
    3
    Views:
    1,344
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page