Help with template object

M

mickey

I have the following template class which I use as a c++ generic callback.

template< class T >
class CallBack
{
public:
typedef void ( T::*Method )( void );

CallBack( void ) : m_classInstance( NULL ), m_method( NULL ){}

CallBack( T* classInstance, Method method ) : m_classInstance(
classInstance ), m_method( method ){}

void operator()( void ) const
{
if( m_classInstance != NULL && m_method != NULL )
( m_classInstance->*m_method )();
};

private:
T* m_classInstance;
Method m_method;
};

I have another class which has a method that takes a template class as a
param...

class foo
{
public:
foo( void );

template< class T >
const boolean Load( CallBack< T > const& callBack, const int16
imgResID )
{
return TRUE;
}

CallBack<class T> m_callback; // member I would like to assign the
callback refernce to
}

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.

Thanks.
 
M

mickey

red said:
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.
 
R

red floyd

mickey said:
I have the following template class which I use as a c++ generic callback.

template< class T >
class CallBack
{
public:
typedef void ( T::*Method )( void );

CallBack( void ) : m_classInstance( NULL ), m_method( NULL ){}

CallBack( T* classInstance, Method method ) : m_classInstance(
classInstance ), m_method( method ){}

void operator()( void ) const
{
if( m_classInstance != NULL && m_method != NULL )
( m_classInstance->*m_method )();
};

private:
T* m_classInstance;
Method m_method;
};

I have another class which has a method that takes a template class as a
param...

class foo
{
public:
foo( void );

template< class T >
const boolean Load( CallBack< T > const& callBack, const int16
imgResID )
{
return TRUE;
}

CallBack<class T> m_callback; // member I would like to assign the
callback refernce to
}

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

TB

mickey sade:
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;
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top