mickey sade:
red floyd wrote: mickey wrote:
I have the following template class which I use as a c++ generic
callback.
<code snipped>
I would like class foo to have a member to which I can assign the
callback reference passed in the Load method.
My problem is that the compiler complains about the difference in
types between m_callback and the callback param. I am not sure how to
declare the m_callback.
I believe you have to make foo a template as well.
i.e.:
template <typename T>
class foo
{
public:
foo( void );
const boolean Load( CallBack< T > const& callBack,
const int16 imgResID )
{
return TRUE;
}
CallBack<T> m_callback; // member I would like to assign the
};
Also, should m_callback be a reference?
Red,
Thanks for your reply. m_callback needs to be a copy, not a reference,
as the passed in reference will be destroyed after the call to Load.
The foo class is specific and making it into a template would create a
slew of compiling problems for my existing project.
Then add another non-template base class which declares operator() virtual:
class CallBackBase {
public:
virtual void operator()() const throw () = 0;
};
template<typename T>
class CallBack : public CallBackBase {
public:
void operator()() const throw () {
}
};
class Foo {
public:
template<typename T>
void Load(CallBack<T> const & callBack) throw (std::bad_alloc) {
// Copying
m_callback = new CallBack<T>(callBack);
}
void Call() {
(*m_callback)();
}
CallBackBase * m_callback;
};
--
TB @ SWEDEN