I've following code and i want to store MyClass<int>,MyClass<float>
... in a vector<BaseClass> mItems
then recover the original class.
I'm trying to figure out if there is a way in C++ to cast from base class to templated class, but still now i can not find the way how to do it.
MyClass<int> lRecover1 = dynamic_cast<MyClass<int>>(mItems.at(0));
This doesn't compile, and I don't understand why. Here is my code if it helps:
#include <vector>
class BaseClass
{
public:
virtual ~BaseClass() {}
};
template<typename T>
class MyClass : public BaseClass
{
public:
MyClass()
{
}
~MyClass()
{
}
};
void main()
{
std::vector<BaseClass> mItems;
MyClass<int> lItem1, lItem2;
MyClass<float> lItem3, lItem4, lItem5;
MyClass<double> lItem6, lItem7;
//PROBLEM: I would like to recover the original class
MyClass<int> lRecover1 = dynamic_cast<MyClass<int>>(mItems.at(0));
}