Pointer to a Member Function

This is a discussion on Pointer to a Member Function within the C++ Programming forums, part of the General Programming Boards category; Code: Pointer myFuncPtr = CStuff::Method; Should be (to be 100% standard): Code: Pointer myFuncPtr = &CStuff::Method; Code: stuff.myFuncPtr(5); Should be: ...

  1. #16
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    Pointer myFuncPtr = CStuff::Method;
    Should be (to be 100% standard):
    Code:
    Pointer myFuncPtr = &CStuff::Method;
    Code:
    stuff.myFuncPtr(5);
    Should be:
    Code:
    stuff.*myFuncPtr(5);
    (the pointer has to be dereferenced to be used)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #17
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    I'm still getting an error:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class CStuff
    {
        public:
            int Method(int Arg);
    };
    
    typedef int (CStuff::*Pointer)(int);
    
    int main()
    {
        Pointer myFuncPtr = &CStuff::Method;
        
        CStuff stuff;
        
        int i = stuff.*myFuncPtr(5);
        cout<<i<<endl;
        
        cin.get();
        return 0;
    }
    
    int CStuff::Method(int Arg)
    { 
        return Arg; 
    }
    The error is (using Dev-C++):

    22 C:\Documents and Settings\John Aurandt\My Documents\Source Folder\FunctPtrTest2.cpp must use .* or ->* to call pointer-to-member function in `myFuncPtr (...)'
    Last edited by homeyg; 04-02-2006 at 12:13 PM.

  3. #18
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I suggest that you go back and examine the format in my earlier post. The format Magos posted does not work for me either.

    You should also take that long error message out of code tags and put it in quote tags so that that the screen width returns to a normal size.
    Last edited by 7stud; 04-02-2006 at 11:55 AM.

  4. #19
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Yeah if you put the parentheses around the dereferenced pointer in the call, it works.

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    Pointer myFuncPtr = CStuff::Method;
    Should be (to be 100% standard):
    Code:
    Pointer myFuncPtr = &CStuff::Method;
    Code:
    stuff.myFuncPtr(5);
    Should be:
    Code:
    stuff.*myFuncPtr(5);
    Is that right? C supports all four.
    Code:
    user@0[~]$ cat functionpointer.c
    #include <stdio.h>
    
    void func(void) {
        puts("func()");
    }
    
    int main(void) {
        void (*f1)(void) = func, (*f2)(void) = &func;
    
        f1();
        (*f2)();
    
        return 0;
    }
    user@0[~]$ gcc -W -Wall -ansi -pedantic -O2 -s -o functionpointer functionpointer.c
    user@0[~]$ cp functionpointer.c functionpointer.cpp
    user@0[~]$ g++ -W -Wall -ansi -pedantic -O2 -s -o functionpointer_cpp functionpointer.cpp
    user@0[~]$
    It seems like C++ supports all forms, too . . . .
    Last edited by dwks; 04-02-2006 at 03:17 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by dwks
    Is that right? C supports all four.
    Code:
    user@0[~]$ cat functionpointer.c
    #include <stdio.h>
    
    void func(void) {
        puts("func()");
    }
    
    int main(void) {
        void (*f1)(void) = func, (*f2)(void) = &func;
    
        f1();
        (*f2)();
    
        return 0;
    }
    user@0[~]$ gcc -W -Wall -ansi -pedantic -O2 -s -o functionpointer functionpointer.c
    user@0[~]$ cp functionpointer.c functionpointer.cpp
    user@0[~]$ g++ -W -Wall -ansi -pedantic -O2 -s -o functionpointer_cpp functionpointer.cpp
    user@0[~]$
    It seems like C++ supports all forms, too . . . .
    We're talking about member functions for classes, so C is irrelevant.

  7. #22
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    have a read of the function pointer tutorials.

    you might also want to have a look at boost.function.
    it's much more flexible and has a nicer syntax.

    which would you rather use?
    Code:
    class MyClass
    {
    public:
        void foo(float x, char y, char z)
        { }
    };
    
    int (MyClass::*pt2Member)(float, char, char) =  &MyClass::foo;
    MyClass x;
    (x.*pt2Member)(1.0, 'a', 'b');
    
    // or 
    
    MyClass x;
    boost::function<void> f = boost::bind(&MyClass::foo, &x);
    f(1.0, 'a', 'b');
    This does come at a small performance cost but the flexibility and readability far outweight this IMNSHO.
    Last edited by ChaosEngine; 04-02-2006 at 08:16 PM. Reason: forgot the class definition
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    We're talking about member functions for classes, so C is irrelevant.
    You said:
    Code:
    Pointer myFuncPtr = CStuff::Method;
    Should be (to be 100% standard):

    Code:
    Pointer myFuncPtr = &CStuff::Method;
    Both work. And both are "100% standard".

    Besides, C has everything to do with it. The reason C++ has two different syntaxes for the same thing is that it was inherited from C. The reason C has both methods is a long story.

    In C, and C++, given func is declared as
    Code:
    void (*func)(void)
    , &func is the same as func, and func() is the same as (*func)(). It's part of the language.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #24
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Both work. And both are "100% standard".
    I read somewhere (when searching for info on member-pointers) that leaving the & out is non-standfard, and that GCC doesn't even allow it to be missing. I don't have GCC though so I cannot confirm it.

    and func() is the same as (*func)()
    Plain function pointers, sure. But method pointers need the (C.*M)() rather than C.M(), or the compiler (in my case MSVC2003) thinks I'm trying to call a non-existing method rather than use the pointer.

    And yes the parantehses needs to be there, forgot it in my earlier post.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #25
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by dwks
    In C, and C++, given func is declared as
    Code:
    void (*func)(void)
    , &func is the same as func, and func() is the same as (*func)(). It's part of the language.
    true, but as has already been mentioned, we're not talking about free function pointers here, we're talking about member function pointers, which have a different syntax.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  11. #26
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by dwks
    You said:

    Both work. And both are "100% standard".

    Besides, C has everything to do with it. The reason C++ has two different syntaxes for the same thing is that it was inherited from C. The reason C has both methods is a long story.

    In C, and C++, given func is declared as
    Code:
    void (*func)(void)
    , &func is the same as func, and func() is the same as (*func)(). It's part of the language.
    Dev-C++ won't compile when you use

    Code:
    funcPtr = Blah::func;
    or

    Code:
    myClass.*funcPtr(blah);

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21