Vector of Template

I'm trying to make a vector of template. However, I want to store the template with different data types. For instance:

class A {};
template<dataType> class B : public A {
private:
dataType x;
public:
dataType getX(){return x;};
void setX(dataType X){x=X;};
};

vector<A*> vectorOfA;
B<uint>* aa;
B<double>* bb;
vectorOfA.push_back( A(aa) );
vectorOfA.push_back( A(bb) );

This is not a problem and seems to compile without problems. I need to extract the "dataType" from the template elements held in the vector. For instance I have another template function that I want to use on the data member "x" in class B:

Function<dataType>( (B<dataType>*)vectorOfA.at(0)->getX() );

So I hope this makes is clear. I have a vector of pointers to A, but were originally pointers to B type objects. This allows me to have an array of B objects that have different template "dataTypes".

However, I want to later figure out for a given entry in the vector, what "dataType" was it declared with.

Thanks for any help in this.
It's typical to declare a typedef you can refer to later on. For example:

1
2
3
4
5
template <class T>
class Number
{
public:
	typedef T value_type;


You can then use value_type in your code. It's available on all STL containers.
But that doesn't solve the original problem. Once a B<T>* is casted to/stored in an A*, T is lost forever.

Topic archived. No new replies allowed.