我在C++ 11和升压编程的情况下,我想实现某种框架,其中一个人只需要从Base类继承,并实现()方法 ,这可能取决于其他继承。 然后应该在主函数中自动创建这些继承,而不需要程序员进行任何修改。自动创建继承
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;
}
我认为这应该是可以与一些花式元编程,但如何?
编辑:澄清
目前尚不清楚你真正想要什么 - 在运行时创建一些As和Bs,或者在运行时创建新类的类型(使用编译时元编程...?) – doctorlove
最后,我想有一个数组包含每个继承的一个实例(或指针)。 – Paul
我想我已经找到了解决我的问题[这里] [1] [1]:http://stackoverflow.com/questions/17410942/c-automatic-instantiation-of-derived-classes – Paul