pointers to class functions?

This is a discussion on pointers to class functions? within the C++ Programming forums, part of the General Programming Boards category; errhh... is it possible to make a pointer that can contain a pointer to function inside a class not yet ...

  1. #1
    btq
    Guest

    pointers to class functions?

    errhh...
    is it possible to make a pointer that can contain a pointer to function inside a class not yet defined?
    like:
    Code:
    struct Linked{
        void (*pt2member)();
        Linked *next;
    }
    
    class MyClass{
         Linked *root;
         void myFunct();
         MyClass();
    }
    
    MyClass::MyClass()
    {
         root = new Linked;
         root->pt2member=(*this.*myFunct)();
    }
    obviously the above don't work and I kinda assume you can't get this to work this way. I guess you get the pictuer: for it to work I have to set the void (*pt2member)(); to void (myClass::*pt2member)(); but (I suspect) since myClass isn't defined before the Linked...yeah well.
    I just want to be assured this style doesn't work before moving on...
    but then again maybe it can work...?)

    I guess the question really is: do you have to declare the pointer to a class's function to be a pointer to the functions of that class
    or can you just use a regular functionpointer somehow?
    like: void (*funct)(); can't point to a class's member function?
    thanks a bunnnchhhhhh
    /btq

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    try this

    ::edit:: Added comments
    Code:
    // prototype/declare the class?
    class MyClass;
    
    // typedef a function pointer to a member of MyClass taking no arguements and returning void
    typedef void (MyClass::*Fptr)(void);
    
    struct Linked{
    // this is the function pointer call like "pt2member();"
        Fptr pt2member;
        Linked *next;
    };
    
    class MyClass{
    	MyClass();
        Linked *root;
    // the function to be pointed to
        void myFunct(void); 
    };
    
    MyClass::MyClass()
    {
         root = new Linked;
    // when assining a function to a function pointer leave of the ()
         root->pt2member= this->myFunct;
    }
    
    void MyClass::myFunct(void)
    {
    
    }
    Last edited by no-one; 08-19-2002 at 04:20 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 12:00 PM
  2. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Class function pointers
    By VirtualAce in forum C++ Programming
    Replies: 40
    Last Post: 02-16-2005, 11:55 PM
  5. DLL class member functions
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 06:59 AM

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