Archived

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

rjacobson

Calling a function pointer from a templated class

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
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