确保静态方法在main()之前调用, [英] Ensuring that a static method gets called before main()

查看:215
本文介绍了确保静态方法在main()之前调用,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作类的集合,我需要能够用一个工厂动态构造这些类的实例。这背后的原因是新工人类经常写,我宁愿不必每次添加新的工人类时更新每个工人类型的工厂类。

I have a collection of worker classes, and I need to be able to construct instances of these classes dynamically with a single factory. The reasoning behind this is that new worker classes are written frequently and I'd rather not have to update a factory class per worker type every time I add a new worker class.

这个方法目前的工作原理如下:我有一个类 WorkerImplementationList 用一个静态方法:

The way that this currently works is as follows: I've got a class called WorkerImplementationList with a static method:

template <typename T>
    WorkerImplementationList::registerWorkerFactoryMethod<T>(const std::string&)

在内部数据结构中的 T :: newInstance 的指针,稍后将由工厂检索。在每个工作类都有一个static int _dummyInstanceRegistrationVariable 。在每个工人类的.cc文件中,我有以下行(以 FooWorker 为例):

that stores a pointer to T::newInstance in an internal data structure that will be retrieved by the factory later. In each of the worker classes have a static int called _dummyInstanceRegistrationVariable. In each of the worker classes' .cc files, I have the following line (using FooWorker as an example):

int FooWorker::_dummyInstanceRegistrationVariable =
    WorkerImplementationList::registerWorkerFactoryMethod<FooWorker>("FooWorker");

我想要发生的是静态变量在任何实例之前被初始化类被构造。这似乎工作很好,当工人类编译成包含 main()的同一个二进制文件。但是,当 FooWorker 位于库中时(例如 libblahapp_workers.a ), _dummyInstanceRegistrationVariable 直到 main()之后才被初始化(假设当

What I'd like to have happen is for the static variable to be initialized before any instances of the class are constructed. This seems to work just fine when the worker class is compiled into the same binary that contains main(). However, when FooWorker is in a library (say libblahapp_workers.a) and the main executable links that library, it looks like _dummyInstanceRegistrationVariable isn't initialized until after main() starts (I assume it's initialized when the first instance of FooWorker is constructed), which is too late for my purposes.

我也尝试过构造一个 FooWorker c> WorkerImplementationRegisterer< T> 在全局范围中的对象,当其被构造时注册适当的工人类型,但是当工人类在外部的库中时,我遇到问题, main();在 main()开始之前未构建全局范围的对象。

I've also tried constructing a WorkerImplementationRegisterer<T> object in global scope that registers the appropriate worker type when it's constructed, but here again I run into problems when the worker class is in a library external to main(); that globally-scoped object isn't constructed before main() begins.

静态链接我的工作库这个问题?有没有更优雅的解决方案,我错过了?

Would statically linking my worker libraries solve this problem? Is there a more elegant solution that I'm missing? Any help you could give me would be greatly appreciated.

推荐答案

这听起来像是链接器只提取它需要的对象,不用全局变量拉一个。那么全局根本不存在,所以初始化顺序是一个问题。

This sounds like the linker pulling in only the objects it needs, and not pulling in the one with the global variable. Then the global simply doesn't exist, so initialization order is a moot point.

这是静态库的一个真正的问题,没有通用的解决方案。

It's a real problem with static libraries, with no universal solution.

由于链接器从库中获取了一个完整的编译单元(在我看过的所有系统上),你可能想出一些方法把这个全局变量需要一个编译单元。

Armed with the knowledge that the linker grabs an entire compilation unit from the library at once (on all systems I've seen), you can probably figure out some way to put this global variable into a compilation unit that's needed.

这篇关于确保静态方法在main()之前调用,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆