Pointers to member functions

Discussion in 'C++' started by Protoman, Jul 1, 2006.

  1. Protoman

    Protoman Guest

    Why doesn't this code work; it should:

    class Obj
    {
    public:
    void a(){cout << "A!" << endl;}
    void b(){cout << "B!" << endl;}
    void c(){cout << "C!" << endl;}
    };

    int main()
    {
    void (Obj::*pFn)();
    Obj obj;
    pFn=&Obj::a;
    obj->*pFn();
    pFn=&Obj::b;
    obj->*pFn();
    pFn=&Obj::c;
    obj->*pFn();
    system("PAUSE");
    return 0;
    }

    Help? Thanks!!!!!
    Protoman, Jul 1, 2006
    #1
    1. Advertising

  2. * Protoman:
    > Why doesn't this code work; it should:


    Protoman, you miserable old troll, /when/ are you going to read the FAQ
    item on how to post?

    Tell what you expect, and what actually happens.

    Be precise, include the actual error message(s) by copying and pasting.


    > class Obj
    > {
    > public:
    > void a(){cout << "A!" << endl;}
    > void b(){cout << "B!" << endl;}
    > void c(){cout << "C!" << endl;}
    > };


    OK, except for lack of indentation.


    > int main()
    > {
    > void (Obj::*pFn)();
    > Obj obj;
    > pFn=&Obj::a;
    > obj->*pFn();


    'obj' is not a pointer, so '->' is not applicable ('.' is).

    Also, you need parentheses.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jul 1, 2006
    #2
    1. Advertising

  3. > * Protoman:
    > > void (Obj::*pFn)();
    > > Obj obj;
    > > pFn=&Obj::a;
    > > obj->*pFn();


    Alf P. Steinbach wrote:
    > 'obj' is not a pointer, so '->' is not applicable ('.' is).
    >
    > Also, you need parentheses.


    yes.. as the following code.

    (obj.*pFn)();

    -- Murali Krishna
    Murali Krishna, Jul 1, 2006
    #3
  4. Protoman

    Protoman Guest

    Murali Krishna wrote:
    > > * Protoman:
    > > > void (Obj::*pFn)();
    > > > Obj obj;
    > > > pFn=&Obj::a;
    > > > obj->*pFn();

    >
    > Alf P. Steinbach wrote:
    > > 'obj' is not a pointer, so '->' is not applicable ('.' is).
    > >
    > > Also, you need parentheses.

    >
    > yes.. as the following code.
    >
    > (obj.*pFn)();
    >
    > -- Murali Krishna


    I figured that one out a couple hours ago, but my DSL and phone lines
    were being upgraded, so I couldn't post right away. Anyway, can't you
    use pointers to member functions to call functions based on a string
    arg?
    Protoman, Jul 1, 2006
    #4
  5. Protoman

    Luke Meyers Guest

    Protoman wrote:
    > Anyway, can't you
    > use pointers to member functions to call functions based on a string
    > arg?


    The number and type of the arguments of a member function are, of
    course, irrelevant to whether you can call said member function via a
    PMF. Are you asking whether you can somehow specify the name of the
    function as a string, and derive a PMF therefrom? Unless you implement
    it yourself, no, you cannot.

    By "implement it yourself," I mean for example something like:

    class Foo { void fun() {} };
    typedef Foo::*() pmfFoo;
    std::map<std::string, pmfFoo> lookupTable;
    lookupTable["fun"] = &Foo::fun;
    Foo f;
    f.*lookupTable["fun"]();

    I didn't compile this, so haven't found all the syntax errors, but you
    get the idea.

    Luke
    Luke Meyers, Jul 1, 2006
    #5
    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. Hamish
    Replies:
    3
    Views:
    576
    Alf P. Steinbach
    Jan 25, 2008
  2. Hicham Mouline
    Replies:
    0
    Views:
    432
    Hicham Mouline
    Apr 23, 2009
  3. Hicham Mouline
    Replies:
    1
    Views:
    414
    Michael DOUBEZ
    Apr 24, 2009
  4. paul
    Replies:
    8
    Views:
    707
    Alf P. Steinbach
    Apr 30, 2009
  5. cerr

    pointers, pointers, pointers...

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

Share This Page