我正在用c++11和boost编程,我正在尝试实现某种框架,其中有人只需要从 类 Base继承并实现method(),这可能取决于其他继承。这些继承应该在主函数中自动创建,程序员无需进行任何修改。
class Base
{
public:
virtual void method() = 0;
}
class A : public Base
{
public:
static int state = 0;
void method() //
{
//do something with B
A::state = B::state * 3;
}
}
class B : public Base
{
public:
static int state = 0;
void method() //
{
//do something with A
B::state = A::state * 2;
}
}
int main()
{
//Access to an Array containing ONE pointer to each Inheritance
vector<Base*> inheritancesOfBase;
inheritancesOfBase.push_back(new A); // <- should be done automatically
inheritancesOfBase.push_back(new B); // <- should be done automatically
//PSEUDOCODE
for_each(base* pInh in inheritancesOfBase)
{
pInh->method();
clones.push_back(pInh->clone());
}
return 0;
}
我认为这应该可以通过一些花哨的元编程来实现,但是如何呢?
编辑:澄清