[C++] Templates and STL Help

Started by
7 comments, last by Nitage 17 years, 11 months ago
I have 2 classes
template <class T>
class IGO;

template <class T>
class MapGO : IGO<T>;
IGO is the interface. I want to hold a vector, list, whatever of IGO objects.
std::vector<IGO*> components;
MapGO<...> component;
components->push_back(component);
Gives error:
cannot convert parameter 1 from 'MapGO<T> *' to 'IGO *const &'
Is this possible?
Anthony Umfer
Advertisement
Does this work?

std::vector<IGO<...> *> components;MapGO<...> component;components->push_back(&component);

my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
Classes use private inheritance by default, which prevents polymorphic use. You may want to try template <class T> class MapGO : public IGO<T>; instead.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Actually, does your base class have to be a template?

You could simplify things greatly doing it this way:

class IGO;template <class T>class MapGO : public IGO;...std::vector<IGO*> components;MapGO<...> component;components->push_back(&component);
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
No luck with the above, but thanks for help.

Specifying IGO<type> when declaring the vector might work but would defeat the point.

Good catch on the ": public," that probably would've caused headaches later on.

Seems like vector wants to know the size of the object...even though it's just a pointer. Looks like it's back to void* for now.
Anthony Umfer
Quote:Original post by CadetUmfer
Specifying IGO<type> when declaring the vector might work but would defeat the point.


Ah, yes, I had overlooked that one. A class template is not a class; only instantiations of the template are. IGO is not a class, IGO<int> is.

Quote:Looks like it's back to void* for now.


Consider boost::any, fusion, or using a non-template base class as Verg suggested.


"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
IGO is not a class, IGO<int> is.


Yup, that's it. C++ Templates != C# Generics. Even void* wouldn't work, can't case to IGO*.

But that's ok, I found another way to go about the problem.

Anthony Umfer
Another obvious approach is declaring a template:

template <typename T> class IGO_Components_Whatever{    typedef IGO<T> IGO_type;    std::vector<IGO_type*> components;}

Omae Wa Mou Shindeiru

You could always have IGO<T> derive from IGO_BASE.

This topic is closed to new replies.

Advertisement