Sign in to follow this  

Address of member function?

This topic is 4310 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

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4310 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this