template class in a STL.

Hello.

I have a class with a template member:

template<typename T>
class TalkFirmware {
public:
funcs_talkToHardware();
funcs_convert_Hardware_Bytes()

private:
T m_Value;
};


Now, I want to put all types into a single vector.

How do I do so? Is there a baseclass I can have TalkFirmware derive from
which can help out? Needing some help.

thanks!

I think that boost::any class may just do the trick or me, but how do I do the following:

void moo( int i);
void moo(double d);


double dd;
int ii;

boost::any aa = dd;

moo(aa); // doesn't work. no moo(boost::any).

Is there a way to do something like:

moo(aa.get()) // calls the right moo()



thanks.
closed account (zb0S216C)
DummyVar wrote:
"Now, I want to put all types into a single vector."

When a template is instantiated, a new type is created. For instance:

1
2
template class TalkFirmware<int>;  // TalkFirmware<int> is a type.
template class TalkFirmware<char>; // TalkFirmware<char> is a type. 

The two above types are completely different. With that in mind, the std::vector would only be able to store a single TalkFirmware type. It is possible to store multiple types with inheritance, however:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct Base
{
    // ...
};

template <typename T>
struct Child : public Base
{
    Child(const T &Init = T()) : Member__(Init) { }
    T Member__;
};

int main()
{
    std::vector<Base *> MyVector;

    MyVector.push_back(&Child<int>(44));
    MyVector.push_back(&Child<float>(1.3F));
}

The problem with this is that it's so restrictive, it's useless.

Wazzak
Last edited on
Topic archived. No new replies allowed.