Advise sought for container type
given the class like the one below: template <typename T> class animal { public: animal (const char* ofname) : _name(ofname) {} private: typedef T _T; std::string _name; _T behavior; } class MeatEater {}; class GrassEather {}; animal<MeatEater> bear ("bear"); animal<GrassEater> rabbit ("rabbit"); How to store "bear" and "rabbit" instances in a vector? Advise is greatly appreciated.
Archie14 wrote:
given the class like the one below:
template <typename T> class animal { public: animal (const char* ofname) : _name(ofname) {} private: typedef T _T; std::string _name; _T behavior; }
class MeatEater {}; class GrassEather {};
animal<MeatEater> bear ("bear"); animal<GrassEater> rabbit ("rabbit");
How to store "bear" and "rabbit" instances in a vector? Advise is greatly appreciated Do you knwo in advance how many "Eater" type you'll have ? If yes : do a std::vector of boost::variant If no : do a std::vector of boost::any
-- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
Well, how would I declare std::vector? std::vector<what>? Using boost::any will require me to do some sort of type discovery at run time. Thanks for pointing the boost::variant to me; that will work. What would be a _proper_ way to design a templated class so it can be stored in a (boost) container?
Archie14 wrote:
Well, how would I declare std::vector? std::vector<what>? Using boost::any will require me to do some sort of type discovery at run time. Thanks for pointing the boost::variant to me; that will work.
std::vector< boost::variant
On Sun, Aug 30, 2009 at 11:47 AM, joel
Archie14 wrote:
Well, how would I declare std::vector? std::vector<what>? Using boost::any will require me to do some sort of type discovery at run time. Thanks for pointing the boost::variant to me; that will work.
std::vector< boost::variant
> v;
Or instead of Boost.Any, create a base class for it all called like AnimalBase that is non-templated, then your above templated Animal class will be as it is, but subclasses from Animal, then put abstract calls or whatever similar functionality in AnimalBase to link it all. Variant is faster all around if you know all of your types before hand.
participants (3)
-
Archie14
-
joel
-
OvermindDL1