- 论坛徽章:
- 0
|
本帖最后由 netaha 于 2015-01-10 21:29 编辑
问题背景是这样的,原先只有一个Child类,实现单例模式如下:
typedef enum EState{
Start,
Stop,
End
}EState;
class Child
{
public:
static Child& GetInstance()
{
static Child instance;
return instance;
}
string EStateAsText(EState state)
{
return EStateTotxt[state];
}
private:
std::map<EState,string> EStateTotxt;
~Child(){};
Child()
{
EStateTotxt[Start] = "Start";
EStateTotxt[Stop] = "Stop";
EStateTotxt[End] = "End";
};
};
// call
Child::GetInstance().EStateAsText((EState)1)
目前有一个变动,需要一个Base类,Child当中的部分数据需要移动到Base当中,Base类大概功能和之前的Child类似,
两者的区别只是,Base类会放在一些通用的code当中来使用,而Child类会放在一些特殊化的code当中使用,这里Child类要继承Base类。
C++新手,请问Base类能做成单例模式不?
还是直接将Base类里面的数据成员直接做成类成员,在通用的code当中直接通过Base类去引用,在一些特殊化的code当中保留原先不变,仍使用Child单例模式,
Child::GetInstance().EStateAsText((EState)1)这样进行引用。
补充:
还请多多指教,thanks
|
|