473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with template object



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.


Jan 24 '06 #1
4 1769
mickey wrote:


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?

Jan 24 '06 #2
red floyd wrote:
mickey wrote:


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?


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.
Jan 24 '06 #3
TB
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
Jan 24 '06 #4

Yeah that works, thank you TB.
Jan 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sks_cpp | last post by:
How can I get the following to work? Are the templates defined incorrectly? I am running this on Visual C++ 6.0. // =============================================== #include <iostream> using...
0
by: Uma Vivek | last post by:
Hi, I have a problem with Flash remoting via .NET . I seem to have all the code right, but flash does not seem to display the data at all. Here's is what Ive done so far... (1) Ive...
0
by: Uma Vivek | last post by:
Hi, I have a problem with Flash remoting via .NET . I seem to have all the code right, but flash does not seem to display the data at all. Here's is what Ive done so far... (1) Ive...
1
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes...
3
by: manuitpro | last post by:
Hi All, I have been using fsockopen() in a function in a class. I am able to pass all the objects in a class to another page except the fsockopen () value. I have put the values of objects from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.