这是我的设计的问题描述.有一个类A(Singleton)可以创建和维护B类的对象.但是有这样的场景,如果某个特定条件出现在B类的对象中,它必须创建另一个B类对象.但是我需要这个创建对象由A类完成.
class B;
class A {
private:
A();
class A *ptr;
public:
A & GetAInstance()
{
if (!ptr)
ptr = new A;
return ptr;
}
void CreateBInstances(std::string name)
{
map[name] = new B(name);
}
};
Class B {
public:
B(std::string name) { }
public:
void checkCondition()
{
if (condition == true)
{
// So here the contidition is hit, I want to create
// another object of class B, but I want to intimate
// class A to do this job for me
}
}
};
我想了解并寻找更好的方法来完成这项肮脏的工作.提前致谢.
A :: GetAInstance应该是静态的.
然后你应该能够做到以下几点
if (condition)
{
A::GetAInstance().CreateBInstance(name);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
293 次 |
| 最近记录: |