Address of member function?

Started by
1 comment, last by InsaneBoarder234 18 years, 1 month ago
I'm using this base functor class and object functor class from the Enginuity articles but I'm having problems creating CObjFunctor objects..

class IBaseFunctor
{
public:
	virtual void operator()() = 0;
};

template <class T>
class CObjFunctor: public IBaseFunctor
{
private:
	//Function pointer typedef
	typedef void (T::*cof_ptr)();

	T *m_Object; //Object pointer
	cof_ptr m_Function; //Function pointer

public:
	//Assign an object and function pointer to this functor
	CObjFunctor(T *pObj, cof_ptr *pFunc) { m_Object = pObj; m_Function = pFunc; }

	//Call the function
	void operator()() { (m_Object->m_Function)(); }
};

My code is as shown below

//Class that uses a CObjFunctor object
class CTimer
{
    //...
    CObjFunctor<CTimer> *m_functor;
    //...
};

//Code where my problems are
CTimer::CTimer()
{
    m_functor = new CObjFunctor<CTimer>(this, &CTimer::Pause);
    //...
}

VC++ 2003 is giving me this error on the line where I call new to create the functor:

error C2664: 'CObjFunctor<T>::CObjFunctor(T *,CObjFunctor<T>::cof_ptr * )' : cannot convert parameter 2 from 'void (__thiscall CTimer::* )(void)' to 'CObjFunctor<T>::cof_ptr * '
        with
        [
            T=CTimer
        ]
        and
        [
            T=CTimer
        ]
        There is no context in which this conversion is possible

Any ideas what I'm doing wrong? The CObjFunctor class should be able to hold a pointer to an object along with a pointer to a member function of that object and then to call said function from anywhere in the program. I appologise for basically posting a load of code, I try to make my requests for help as specific as possible in order to avoid confusion but this time I'm completely stumped and don't have the slightest idea where to start.
Progress is born from the opportunity to make mistakes.

My prize winning Connect 4 AI looks one move ahead, can you beat it? @nickstadb
Advertisement
I'm pretty certain this is your problem:
CObjFunctor(T *pObj, cof_ptr *pFunc) { m_Object = pObj; m_Function = pFunc; }

cof_ptr is already a pointer ot a member function, so the asterisc is unneccessary. You got it right when you defined m_Function. Then, when you actually call that constructor, you pass it a pointer to a member function, instead of a pointer to a pointer to a member function [which you might not even be able to legally create], thus the compile error.

CM
Argh! Hehe, thanks a lot for finding my mistake!
Progress is born from the opportunity to make mistakes.

My prize winning Connect 4 AI looks one move ahead, can you beat it? @nickstadb

This topic is closed to new replies.

Advertisement