Jump to content
  • Advertisement
Sign in to follow this  
CadetUmfer

[C++] Templates and STL Help

This topic is 4415 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Does this work?


std::vector<IGO<...> *> components;

MapGO<...> component;

components->push_back(&component);

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
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.

Share this post


Link to post
Share on other sites
Another obvious approach is declaring a template:


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!