?
=?ISO-8859-1?Q?J=FCrgen_Kaminski?=
Hello all,
I got some legacy code where a template implements a non-templated
interface. These classes are really HUGE, but it boils down to the
following structure:
class BaseClass
{
public:
virtual ~BaseClass() {}
virtual bool Create(size_t size) = 0;
/* lots of other stuff here */
protected:
void* m_pData;
};
template <class T>
class Wrapper : public BaseClass
{
public:
virtual ~Wrapper() { delete (T*) m_pData; }
virtual bool Create(size_t size)
{
m_pData = new T[size];
return (m_pData != 0);
}
T GetElement(int i) { return *((T*)m_pData+i); }
};
The purpose of this is to have collections of Wrappers with different types:
Wrapper<int>* intwrapper = new Wrapper<int>;
Wrapper<float>* floatwrapper = new Wrapper<float>;
intwrapper->Create(10);
floatwrapper->Create(5);
std::vector<BaseClass*> vec;
vec.push_back(intwrapper);
vec.push_back(floatwrapper);
for ( std::vector<BaseClass*>::iterator it = vec.begin();
it != vec.end(); ++it )
{
DoSomething( (*it) );
}
Now what do to with such a pointer? I can't do anything with it!
For example, I want to access the third element in each array and say
something like:
void DoSomething(BaseClass* any)
{
std::cout < "Element 3: " << any->GetElement(3);
}
But this obviously does not work. GetElement can't be virtual, because
BaseClass does not know which type it should return. Making it a
template does not help either, because this means giving up the nice
inhomogeneous container.
Is there some template magic to solve this problem? I know that it's all
screwed up, but there are thousands of LOC using this class.
Thanks in advance,
Juergen
I got some legacy code where a template implements a non-templated
interface. These classes are really HUGE, but it boils down to the
following structure:
class BaseClass
{
public:
virtual ~BaseClass() {}
virtual bool Create(size_t size) = 0;
/* lots of other stuff here */
protected:
void* m_pData;
};
template <class T>
class Wrapper : public BaseClass
{
public:
virtual ~Wrapper() { delete (T*) m_pData; }
virtual bool Create(size_t size)
{
m_pData = new T[size];
return (m_pData != 0);
}
T GetElement(int i) { return *((T*)m_pData+i); }
};
The purpose of this is to have collections of Wrappers with different types:
Wrapper<int>* intwrapper = new Wrapper<int>;
Wrapper<float>* floatwrapper = new Wrapper<float>;
intwrapper->Create(10);
floatwrapper->Create(5);
std::vector<BaseClass*> vec;
vec.push_back(intwrapper);
vec.push_back(floatwrapper);
for ( std::vector<BaseClass*>::iterator it = vec.begin();
it != vec.end(); ++it )
{
DoSomething( (*it) );
}
Now what do to with such a pointer? I can't do anything with it!
For example, I want to access the third element in each array and say
something like:
void DoSomething(BaseClass* any)
{
std::cout < "Element 3: " << any->GetElement(3);
}
But this obviously does not work. GetElement can't be virtual, because
BaseClass does not know which type it should return. Making it a
template does not help either, because this means giving up the nice
inhomogeneous container.
Is there some template magic to solve this problem? I know that it's all
screwed up, but there are thousands of LOC using this class.
Thanks in advance,
Juergen