0

I have a class with which I'm wrapping some information, for example:

template<typename T>
class Type
{
pubilc:
    Type(const T value) : m_value(value) {}
    T GetValue() const { return m_value; }
private:
    T m_value;
};

And I want to store a bunch of these in a container. But to do this they all need to use the same template param. To work around this I have been doing:

class TypeBase
{}

template<typename T>
class Type : public TypeBase
{
pubilc:
    Type(const T value) : m_value(value) {}
    T GetValue() const { return m_value; }
private:
    T m_value;
};

and storing TypeBase* in my container.

Is there a better way to work around this?

NeomerArcana
  • 1,978
  • 3
  • 23
  • 50
  • 1
    Given the code you posted, if you store `TypeBase*` in a container, how would you utilize each item if you iterate through the container? Not saying that storing `TypeBase*` can't be done, but you need some sort of virtual function in `TypeBase` or additional template magic to make use of the container. – PaulMcKenzie Dec 26 '15 at 07:18
  • So you're wanting to do something like this: `std::vector> container; Type i; Type f; container.push_back(i); container.push_back(f);` .. then iterate over the `container` and perform some action on the underlying type/data? – txtechhelp Dec 26 '15 at 07:25
  • 2
    If the possible `T` are known, `boost::variant` may help. – Jarod42 Dec 26 '15 at 09:11
  • @PaulMcKenzie Yes, I would have some sort of virtual function that actually accomplishes something; the code was just to illustrate what I'm going. – NeomerArcana Dec 26 '15 at 10:21

0 Answers0