C ++自动实例化派生类 [英] C++ Automatic instantiation of derived classes
问题描述
我有一个名为 Base
的抽象基类,其他程序员将为其编写实现。在应用程序的其他部分,我想捕获所有已编写的实现,并为每个实现构造一个实例。如果可以在实现基础之外没有其他指令的情况下完成此操作,那就太好了。但是,我下面的代码要求每个实现自己注册。
I have an abstract base class called Base
that other programmers are to write implementations for. In some other part of the application, I want to catch all implementations that have been written and construct a single instance of each. If this could be done with no additional instructions to others beyond "implement Base", that would be beautiful. However, the code I have below, requires that each implementation register itself. It also doesn't work.
#include <iostream>
#include <vector>
class Base;
std::vector<Base*>* registrationList = new std::vector<Base*>;
class Base {
public:
Base(){}
virtual void execute() = 0;
};
class ImplementationOne: public Base {
public:
ImplementationOne(){registrationList->push_back(this);}
void execute(){std::cout << "Implementation One." << std::endl;}
static int ID;
};
class ImplementationTwo: public Base {
public:
ImplementationTwo(){registrationList->push_back(this);}
void execute(){std::cout << "Implementation Two." << std::endl;}
static int ID;
};
int main(int argc, const char * argv[]){
std::cout << "Registration List size: " << registrationList->size() << std::endl;
for(auto it = registrationList->begin() ; it != registrationList->end() ; ++it){
(dynamic_cast<Base*>(*it))->execute();
}
return 0;
}
我得到以下输出:注册列表大小: 0
,因此很明显这些实现从未实例化。很可能不会发生这种情况,但是我是一个初学者,这是我能想到的最好的方法。我假设 static int ID;
将强制实例化每个实现,然后将其注册。我可以看到 static
不会导致实例化。我将其留在代码中,因为它显示了我的意图。
I get an output of: Registration List size: 0
, so it is clear that the implementations were never instantiated. It is probably obvious that this wouldn't happen, but I am a beginner and this is the best I could come up with. I assumed that static int ID;
would force instantiation of each implementation, which would then register themselves. I can see static
does not result in instantiation. I leave it in my code here since it shows my intent.
我该怎么做才能自动实例化每个实现?
What can I do to get automatic instantiation of each implementation? Is it possible?
推荐答案
添加 static
成员不会 导致实例的生成,它仅声明此类型具有全局变量。但是,您实际上从未定义过这些成员,因此,如果您尝试将它们用于任何用途,则将出现链接器错误。您必须实际实例化一个对象并将其注册。
Adding static
members does not cause an instance to be generated, it merely declares that this type has a "global" variable. You never actually defined these members though, so if you tried to use them for anything you would have had a linker error. You'll have to actually instantiate an object and register that.
一种解决方案可能是简单地要求每种派生类型在启动时注册实例。正如我在此处所示,这实际上很容易做到。 (请注意,我已将全局变量移到静态函数的静态局部。这可以防止您尚未遇到的一些问题,包括为全局变量提供所有者。)
One solution might be to simply require each derived type to register an instance at startup. This is actually quite easily done, as I show here. (Note I moved your global to a static local of a static function. This prevents several problems you haven't run into yet, including providing an "owner" for the global.)
与您的问题无关,您的代码有问题:
Unrelated to your issue, your code has problems:
- 几乎没有任何理由
- 您是从没有虚拟析构函数的类型中多态派生的。
- 您
dynamic_cast< Base *>
,没有任何正当理由。 - 每个派生类都声明但未定义
ID
成员。
- There is almost never any reason to have a pointer to a container.
- You are deriving polymorphically from a type with no virtual destructor.
- You
dynamic_cast<Base*>
for no apperent reason. - Each of your derived classes declares but does not define a
ID
member.
这篇关于C ++自动实例化派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!