Complex polymorphism problem

Hello guys,

I have a complicated problem, and I need your help.

I have a base case,

1
2
3
4
5
class ParamBase
{
string paramValue;
//...
}


and a bunch of class templates with different template parameters.

1
2
3
4
5
6
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.

Any help would be highly appreciated :-)

Last edited on
Could you try stream operations? If value is a builtin type, it should "just work."

For example:
1
2
3
4
#include <sstream>
//...
std::stringstream ss( paramValue );
ss >> value;
Where should I call this? That's the question.
Create a virtual method convert in ParamBase:
virtual void convert() = 0;

Then override it in Param
1
2
3
4
5
virtual void convert() override
	{
		std::istringstream iss(paramValue);
		iss >> value;
	}


This way you can call convert on ParamBase pointers
Never knew I could call the base pure method. Thanks a lot :)
Topic archived. No new replies allowed.