题
语言:C ++工具包:Qt4
我正在使用的工具包有一个名为的静态方法 int QEvent::registerEventType()
注册我自己的事件类型。当我将其子类化时 QEvent
我需要为基类提供这个值。 QEvent::QEvent(int type)
.
在应用程序启动之前使用静态变量调用它可以吗?考虑以下:
//This is all in my .cpp file
static int myEventType; //This will contain my registered type
/*If I create a static instance of this class the constructor
gets called before the main() function starts.
*/
class DoRegisterMyEventType {
public:
DoRegisterMyEventType() {
myEventType = QEvent::registerEventType();
}
};
static DoRegisterMyEventType doRegisterMyEventType;
//Here is the constructor for MyEvent class which inherits QEvent.
MyEvent::MyEvent()
: QEvent(myEventType)
{
}
这到底有多“邪恶”?我可以将整个东西包装在一个命名空间中,以防止污染全局命名空间。
解决方案
静态级初始化是一个巨大的编译器相关的灰度区域,如其他人提及。然而,功能级别初始化不是灰色区域,可用于你的优势。
static inline int GetMyEventType()
{
static int sEventType = QEvent::registerEventType();
return sEventType;
}
MyEvent::MyEvent()
: QEvent(GetMyEventType())
{
}
该解决方案具有registerEventType保证您需要的事件类型之前,即使你静态初始化,这是很好的过程中构建MyEvent被称为财产,但它确实打开你到线程安全问题,如果有可能为MyEvent要在多个线程构成。
下面是一个线程安全版本的基础上的boost :: call_once的:
#include "boost/thread/once.hpp"
static boost::once_flag sHaveRegistered = BOOST_ONCE_INIT; //This is initialized statically, effectively at compile time.
static int sEventType = -1; //-1 is not a valid event
static void DoRegister()
{
sEventType = QEvent::registerEventType();
}
static inline int GetMyEventType()
{
boost::call_once(sHaveRegistered, &DoRegister);
return sEventType;
}
其他提示
自C++初始化以来 跨 TU 这是一个很大的灰色区域,有很大的实施余地,我宁愿完全废弃它,并明确说明何时完成什么。(由于缺乏保证而拒绝初始化顺序类似于 单例类 拒绝全局对象。)具体来说,这意味着任何不能用常量表达式初始化的全局状态(全局变量、静态数据成员和函数局部静态)都必须在一个 TU 中初始化,并且该 TU 是实现 主要的.
在手动情况下,这意味着在包含以下内容的翻译单元中插入和更新代码: 主要的 并在 主要的 本身。此类代码最常见的示例是调用 srand(time(0))
播种 标准::兰德 PRNG。
您可以使用预处理器重构手动代码管理:
// the implementation file for main, could be named main.cpp
#include "whatever_declares_the_real_main.hpp"
#include "global_objects.inc"
int main(int argc, char* argv[]) try {
#include "main_init.inc"
return the_real_main(argc, argv);
// main.cpp has well-defined responsibility:
// initialize global state before passing control to another function, and
// handle return-code or exceptions
// you can modify this, depending on your preference and desired API
// for example:
return the_real_main(std::vector<std::string>(argv+1, argv+argc));
return the_real_main(parse_args(argv+1, argv+argc));
// just make sure to keep main.cpp's responsibility well-defined and
// relatively simple
}
// example handling; depending on your specifics, you might do something
// different, or know how to provide more information:
catch (std::exception& e) {
std::cerr << "abnormal termination: " << e.what() << '\n';
return 1;
}
catch (...) {
std::cerr << "abnormal termination.\n";
return 1;
}
这些 .inc 文件既不是标头也不是实现文件。只要您不使用标头或实现文件常用的扩展名(例如 .h、.hpp、.cc、.cpp 等),确切的文件扩展名并不重要。您可以生成 全局对象.inc 和 main_init.inc 基于文件命名约定,使用包含防护,以便可以包含依赖项(就像包含防护适用于标头一样)。
例如,这两个文件都对应于 myevent.hpp 并将放置在该标题旁边:
// file "myevent.global_inc"
#ifndef INCLUDE_GUARD_37E6F5857F8F47918A7C83F29A9DA868
#define INCLUDE_GUARD_37E6F5857F8F47918A7C83F29A9DA868
#include <QEvent.hpp> // or whatever headers you need
#include "myevent.hpp" // declares the variable defined just below
// (remember you use 'extern' to declare objects without defining them)
int your_namespace::myEventType = QEvent::registerEventType();
#endif
// file "myevent.main_inc"
#ifndef INCLUDE_GUARD_4F1B93D0F4D3402B802CBA433241AA81
#define INCLUDE_GUARD_4F1B93D0F4D3402B802CBA433241AA81
// nothing needed in this case, from what you've shown so far
// this is where you place expressions that would otherwise require a dummy
// global variable to make sure they are executed, but this also allows use
// of temporary variables while includes handle dependency order:
#include "something_else.main_inc" // fake example dependency, which must
{ // be executed first
int temp;
some_func(&temp);
other_func(temp); // not easy to transform this into a global's init
// expression, yet defining it this way is natural, because it's exactly
// how you would do it inside a function
}
#endif
请注意,如果您只需要使用常量表达式进行静态数据初始化,那么这优于所有其他技术。该初始化的主要限制是无法进行函数调用(但实际上更复杂),因此它不适用于您的情况;如果您想了解更多信息,这是 C 可以执行的唯一一种全局变量初始化。
我用的是“静态登记对象”的格局了不少,但你必须明白一个很大的问题 - 你必须确保你与登记的事情,这本身很可能是静态的,是之前的事情创建您注册。由于C ++不保证翻译单元之间的静态顺序施工,这可能是有问题的。一个解决方案是使用所谓的迈耶的Singleton:
class Registry {
public:
static Registry & Instance() {
static Registry r;
return r;
}
...
private:
Registry() {
...
}
};
至于注册表的所有引用都必须通过实例()方法,可以保证所需要的施工顺序。