Execute a function to affect different template class instances
Posted
by
Samer Afach
on Stack Overflow
See other posts from Stack Overflow
or by Samer Afach
Published on 2012-04-06T17:21:24Z
Indexed on
2012/04/06
17:29 UTC
Read the original article
Hit count: 234
I have a complicated problem, and I need help.
I have a base case,
class ParamBase
{
string paramValue;
//...
}
and a bunch of class templates with different template parameters.
template <typename T>
class Param : public ParamBase
{
T value;
//...
}
Now, each instance of Param has different template parameter, double, int, string... etc.
To make it easier, I have a vector to their base class pointers that contains all the instances that have been created:
vector<ParamBase*> allParamsObjects;
The question is:
How can I run a single function (global or member or anything, your choice), that converts all of those different instances' strings paramValue with different templates arguments and save the conversion result to the appropriate type in Param::value. This has to be run over all objects that are saved in the vector allParamsObjects.
So if the template argument of the first Param is double, paramValue has to be converted to double and saved in value; and if the second Param's argument is int, then the paramValue of the second has to be converted to int and saved in value... etc.
I feel it's almost impossible... Any help would be highly appreciated :-)
© Stack Overflow or respective owner