建议使用类厂模式
class Graph
{
public:
virtual void Paint() = 0;
};
class BezierGraph:public Graph
{
//实现
};
class NaturalGraph:public Graph
{
//实现
};
class GraphFactory
{
public:
GraphFactory();
Graph* GetObject(string object)
{
if(object == "bezier")return new BezierGraph;
else if(object == "natural")return new NaturalGraph;
else
//可以抛出异常或返回0
}
};
//使用
GraphFactory gf;
string str;
Graph* pG = gf.GetObject(str);//这里的参数可以代替你要用的条件语句了,你也注意到了吧,条件语句在GraphFactory里呢。
pG->Paint();