General pointer to a class function

Started by
1 comment, last by SiCrane 19 years, 3 months ago
Hi Guys, I've been coding a simple (i.e. im not an experienced programmer) application wrapper for SDL in c++, that simply sets up SDL, the screen, and event loop, and gives the users a few commands to add or remove their functions from the application loop i.e. allowing you to suddenly run a certain menu box if a button is clicked, ontop of your current functions and then giving either the new function or its parent the ability to remove it from excecution. Naturally the next step was to enable this loop for classes so that a menu object could be instanced then immediately adds itself to the loop of excecution, with the same abilities as functions being added to the loop, allowing people to make simple, clean addon modules for the program (normal functions could have conflicting variables etc). However when i pass a function to the loop i use the pointer void (*Function)(). When i try passing a function in a class i.e ( this->Start ) () it passes the class name aswell so it looks like ( ClassMenu::Start ) () on the other end. C++ doesn't like this. Can anyone help with a pointer decleration that will work for all classes? Thanks.
Advertisement
you can't do it that way, what you can do is make a ibasefunc-class, and then inheritd globalfunc, memberfunc etc from that one. then your function take an ibasefunc as an argument. short example:
//abc for func returning Rtemplate<class R>class IBaseFunc{public:	virtual ~IBaseFunc(){}	virtual R operator()() const = 0;};//template for memberfunc to class C returning Rtemplate<class C,class R>class CMemFunc : public IBaseFunc<R>{public:	typedef R (C::*F)();	CMemFunc(C& obj,F func) : m_obj(&obj) ,m_func(func){}	virtual R operator()() const	{		return (m_obj.*m_func)();	}	inline virtual C& getObj() { return m_obj; }	inline virtual F getFunc() { return m_func;	}	inline virtual void setObj(C& obj) { m_obj = &obj; }	inline virtual void setFunc(F func) { m_func = func; }	CMemFunc& operator=(const CMemFunc& rhs)	{			}private:	C* m_obj;	F m_func;};//helperfunction for creating a CMemFunctemplate<class C, class R>IBaseFunc<R>* memFunc(C& obj,R (C::*f)()){	return new CMemFunc<C,R>(obj,f);}


and then your function would just take a IBaseFunc*
reality is only an option
Alternately you can use an existing functor class like boost::function or loki's functor class.

This topic is closed to new replies.

Advertisement