Pointers to Members

Discussion in 'C++' started by Christian Nolte, Feb 1, 2004.

  1. I want to implement dynamic calling of interface methods using pointers to
    members and have some trouble to get it right. An example of what I am
    talking about looks like that:

    ///////////////////////////////////////////////
    class StdInterface {
    public:
    int func(void) {return 1;}
    int func2(void) {return 2;}
    };

    typedef int (StdInterface::*fp)();

    class UseInterface {
    public:
    fp m_func;
    };
    ///////////////////////////////////////////////

    The next code excerpt shows how I want to use the pointer to meber:

    ///////////////////////////////////////////////
    UseInterface *a = new UseInterface();
    StdInterface *b = new StdInterface();

    std::cout << b->func();
    a->m_func = &StdInterface::func;

    // here the compiler error C2065: 'm_func': not declared occurs (MS VC
    ..net):
    std::cout << (a->*m_func)();
    ///////////////////////////////////////////////

    Why does the compiler complain here? I have found an example of how to use a
    pointer to member in Stroustrup (15.5 - Pointers to Members):

    ///////////////////////////////////////////////
    //Example : Stroustrup (15.5 : Pointers to Members)

    typedef int (StdInterface::*Pstd_mem)(); // pointer to member type

    void f(StdInterface* p)
    {
    Pstd_mem s = &StdInterface::func;
    //p->func(); // direct call
    std::cout << (p->*s)() ; // call through pointer to member
    }
    ////////////////////////////////////////////////

    The above code works fine while mine is somehow wrong and I don't have a
    clue on how to fix it. The only difference is, that my pointer is inside a
    class, while the pointer in Stroustrups example is somehow
    "function-scoped"...

    Any help would be very much appreciated!
     
    Christian Nolte, Feb 1, 2004
    #1
    1. Advertising

  2. Christian Nolte

    David Harmon Guest

    On Sun, 1 Feb 2004 21:57:44 +0100 in comp.lang.c++, "Christian Nolte"
    <> was alleged to have written:
    >I want to implement dynamic calling of interface methods using pointers to
    >members and have some trouble to get it right.


    For a start, see section "[33] Pointers to member functions" in Marshall
    Cline's C++ FAQ. It is always good to check the FAQ before posting.
    You can get the FAQ at:
    http://www.parashift.com/c -faq-lite/

    >// here the compiler error C2065: 'm_func': not declared occurs (MS VC
    >.net):
    >std::cout << (a->*m_func)();


    Invoking the function requires an instance of class StdInterface.
    b->*

    m_func is a ordinary member of class UseInterface.
    a->m_func

    Put them together
    b->*(a->m_func)()
     
    David Harmon, Feb 1, 2004
    #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. JFCM
    Replies:
    4
    Views:
    5,742
  2. CoolPint
    Replies:
    8
    Views:
    989
    Jeff Schwab
    Dec 14, 2003
  3. Dave
    Replies:
    3
    Views:
    369
    tom_usenet
    Aug 10, 2004
  4. hdixon
    Replies:
    3
    Views:
    649
    hdixon
    Jul 9, 2006
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    683
Loading...

Share This Page