C++设计问题: Can i query the base classes to find the number of derived classes satisfying a condition

标签 c++ design-patterns

我有一段这样的代码

class Base
{
public:
Base(bool _active)
{
 active = _active;
}
void Configure();
void Set Active(bool _active);
private: 
bool active;
};
class Derived1 : public Base
{
public:
Derived1(bool active):Base(active){}

};

similarly Derived 2 and Derived 3

现在,如果我调用 derived1Object.Configure,我需要检查有多少 derived1Obj、derived2Obj、derived3Obj 处于事件状态。我应该像函数 GetNumberOfActive() 那样在“基”类中添加它吗?

如果实现是这样的:

class Imp
{
public:
 void Configure()
 {
  //Code instantiating a particular Derived1/2/3 Object 
  int GetNumberOfActiveDerivedObj();
  baseRef.Configure(int numberOfActiveDerivedClasses);
 }
prive:
Derived1 dObj1(true);
Derived2 dObj2(false);
Derived3 dObj3(true);
};

我应该计算 Imp 类中的 numberOfActive 派生对象吗?

谢谢

最佳答案

一个简单的可能性:

NumberOfActiveBase 中应该是 static。每次创建对象并且 active 为真时,它都应该递增。

关于C++设计问题: Can i query the base classes to find the number of derived classes satisfying a condition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2943050/

相关文章:

java - 在枚举中添加长方法是不是不好的设计?

design-patterns - 是否可以向 Scala 中的内置类型添加方法?

c++ - 使用 VS2012 未为 Qt 加载引用

c++ - 确实应该关闭的文件共享违规

c++ - 是否可以确保在编译时最多调用一次 constexpr 函数?

.net - 工作单位模式

python - gui应用程序的面向对象设计问题

c++ - 接受const char *列表的字符串类构造函数

c++ - 用于基于语音的命令激活的开源库

c - C 库中的错误报告