Thread: class function pointer

  1. #1
    coder
    Join Date
    Feb 2008
    Posts
    127

    class function pointer

    hello
    Is there a known way to assign a class method to a function pointer?

    I tried this, which doesn't work:
    Code:
    class MyClass {
    	void func ()
    	{
    		...
    	}
    }
    
    int main ()
    {
    	void (*fptr) ();	//function pointer
    	MyClass foo;		//instance
    
    	fptr = foo.func;
    }
    compiler (g++) says:
    error: argument of type ‘void (Class:: )()’ does not match ‘void (*)()’

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Class functions has a special syntax:
    void (MyClass::*)()
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But why would you actually want to do that? The whole point of having classes is that they have member functions. With virtual member functions, you can create the same effect, but avoid the actual function pointer mess - yes, internally the compiler generates roughly the same code, but it's much easier to read and use.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    coder
    Join Date
    Feb 2008
    Posts
    127
    ok thanks
    but, what if I wish to use the pointer with different classes?

  5. #5
    coder
    Join Date
    Feb 2008
    Posts
    127
    Quote Originally Posted by matsp
    But why would you actually want to do that?
    The main use the fpointer to call the function I need
    eg:
    STATE = game -> call Game::main ()
    STATE = menu -> call Menu::main ()

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're thinking in a C way. You probably want something like this in C++.
    Code:
    class base {
    public:
        virtual void main() = 0;  // no really, Elysia, this void main is okay :)
    };
    
    class game : public base {
    public:
        virtual void main() {  // "virtual" not technically required
            // ...
        }
    };
    
    class menu : public base {
    public:
        virtual void main() {  // "virtual" not technically required
            // ...
        }
    };
    
    // and so on
    
    base *p = new menu;
    p->main();
    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.

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Code:
     
    fptr = MyClass::foo.func;
    should work, but i woudlnt recommend it. Why exactly do you need a function pointer to a member function?

    Is thewre some reason you cant make it a friend function to all the clsses its associated with?
    Last edited by abachler; 02-13-2008 at 03:54 PM.

  8. #8
    coder
    Join Date
    Feb 2008
    Posts
    127
    Well, I'm not used with class stuff like virtual functions, inheritance and friendship...
    so I'm trying to learn more about at the moment, since I'm a bit confused now

    However I've encountered another problem using a function-pointer stack:
    Code:
    #include <iostream>
    using namespace std;
    
    #include <stack>
    
    void menu ()
    {
    	cout << "hello\n";
    }
    
    typedef void (*fptr) ();
    
    int main ()
    {
    	stack<fptr> State;
    
    	fptr temp;
    	temp = menu;
    
    	State.push (temp);
    	State.top ();
    
    	return 0;
    }
    this code didn't print "hello": what's wrong?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, you're not calling the function, are you?
    It should be something like
    State.top()();
    or maybe
    ( State.top() )();
    Seeing as top is a function that returns a reference to the object at the top of the stack.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    coder
    Join Date
    Feb 2008
    Posts
    127
    Aaargh you're right
    I was missing that detail indeed
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM