通过库进行静态变量初始化 [英] Static variable initialization over a library
问题描述
我正在工厂中添加类型,但是,如果未在显示的.exe中(编译时)显式初始化该类,则不会将类型添加到工厂。这是由于这样的事实,即静态调用是如何进行的。有人对如何解决此问题有任何建议吗?以下是我要放入一个库中的五个非常小的文件,然后一个.exe文件将其称为该库。如果有关于如何使它起作用的建议,或者可能是更好的设计模式,请告诉我。这基本上就是我要寻找的东西
I am working on a factory that will have types added to them, however, if the class is not explicitly instiated in the .exe that is exectured (compile-time), then the type is not added to the factory. This is due to the fact that the static call is some how not being made. Does anyone have any suggestions on how to fix this? Below is five very small files that I am putting into a lib, then an .exe will call this lib. If there is any suggestions on how I can get this to work, or maybe a better design pattern, please let me know. Here is basically what I am looking for
1)可以接受类型的工厂
1) A factory that can take in types
2)自动要在.cpp类文件中进行注册,所有注册代码都应该在.cpp类中(例如下面的示例,RandomClass.cpp),而没有其他文件。
2) Auto registration to go in the classes .cpp file, any and all registration code should go in the class .cpp (for the example below, RandomClass.cpp) and no other files.
BaseClass.h: http://codepad.org/zGRZvIZf
BaseClass.h : http://codepad.org/zGRZvIZf
RandomClass.h: http://codepad.org/rqIZ1atp
RandomClass.h : http://codepad.org/rqIZ1atp
RandomClass.cpp: http://codepad.org/WqnQDWQd
RandomClass.cpp : http://codepad.org/WqnQDWQd
TemplateFactory.h: http://codepad.org/94YfusgC
TemplateFactory.h : http://codepad.org/94YfusgC
TemplateFactory.cpp: http://codepad.org/Hc2tSfzZ
TemplateFactory.cpp : http://codepad.org/Hc2tSfzZ
推荐答案
作为一般经验法则,应用程序不包含库中的静态或全局变量,除非它们被应用程序隐式或显式使用。
As a general rule of thumb, an application do not include static or global variables from a library unless they are implicitly or explicitly used by the application.
有一百种不同的方法可以重构。一种方法是将静态变量放在函数内部,并确保调用了该函数。
There are hundred different ways this can be refactored. One method could be to place the static variable inside function, and make sure the function is called.
这篇关于通过库进行静态变量初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!