Sign in to follow this  
CadetUmfer

[C++] Templates and STL Help

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Classes use private inheritance by default, which prevents polymorphic use. You may want to try template <class T> class MapGO : public IGO<T>; instead.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this