Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

rjacobson

Calling a function pointer from a templated class

This topic is 5588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok. Hopefully this is an easy question for someone. What I''m trying to do is call an arbitrary function from a templated object. Everything seems to compile fine until it gets to the actual function pointer call, at which point it pukes out: " in method ''void A::setFunc( void(B::*)() )'' cannot convert a pointer of type ''A'' to a pointer of type ''B'' must use .* or ->* to call pointer to member function in ''A::func(...)'' " I''ve already spent hours searching through google for answers, but couldn''t find anything mentioning this situation. Here''s a simple version of the source code I''m using.... Maybe it''s a simple solution and I''ve just been staring at the code for too damn long... In any case, can anyone tell me what''s going on??

template<class T>
class A
{
public:
    // function pointer

    void(T::*func)(void);
    
    // set the function pointer and call it.

    void setFunc( void(T::*f)(void) ) {
        func = f;

        // THIS HERE GIVES ME THE GRIEF        

        func();         
    };
            
    A(){};    
};

class B
{
public:
    
    A<B>* Aobject;

    // this is the function that needs to be called from A<class T>

    void doFunc() {
        // do stuff        

        cout << "Doing stuff" << endl;
    };
    
    void setupFunc() {
        Aobject = new A<B>();
        Aobject->setFunc( &B::doFunc );        
    };
    
    B(){};
};



int main()
{
  B Bobject;
  Bobject.setupFunc();  
     
  
  return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
You need to provide a B object in order to call a B member function, the member function pointer is not enough by itself. If it''s appropriate for your situation you can store a pointer to a B object in A<B> and then call the member function using ->*.

Share this post


Link to post
Share on other sites
Thanks a lot.. for some reason that hadn''t occurred to me, but it makes sense now that you''ve mentioned it..

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!