I have a class template which inherits from a base class and the template class have a member variable that can be of any type( just like in stl continers).
class declartion:
class fooBase
{
public:
int x;
int y;
};
template <class T>
class fooTemplate : public fooBase
{
public:
T data;
};
now after I instantize the template class I save a pointer to it in a vector that holds pointers of the base class type.
all works fine till I wanted to get the pointer to my template class back and found out that I can't cast it back unless when I call the function to get the pointer back I tell it to what type of the template to cast it.
int main ()
{
std::vector<fooBase*> foos;
fooTemplate<int>* fooTemp = new fooTemplate<int>;
foos.push_back(fooTemp);
getTemplate<int>(foos[0]);//works fine
getTemplate(foos[0]); //compile error for not telling it the template type
delete fooTemp;
return 0;
}
my question is if there is a way to save type of the template so I don't need to tell the function the type of the template ?
or some way to catch if I by mistake I cast it to the wrong template type ?
also could I be just thinking of this all thing in the wrong way ?