在c++中与Objective-C混合的静态初始化

时间:2022-09-07 08:21:58

I have an iPhone game, a port from C++ on other platforms. So the user interface is coded in Obj-C, but the core logic of the program is in several C++ files. Some of those C++ files have numerous global variables with initializers, at the top.

我有一个iPhone游戏,是c++在其他平台上的一个端口。所以用户界面是用object -C编写的,但是程序的核心逻辑是在几个c++文件中。其中一些c++文件在顶部有许多全局变量,带有初始化器。

Surprise! Debugging would suggest that the initialization never took place. As if Obj-C linkage doesn't know to call the C++ static initialization chain. Anybody know a fix for this? A way to manually force those initialization routines to get called?

惊喜!调试表明初始化从未发生过。好像object -C链接不知道调用c++静态初始化链。有人知道解决方法吗?手动强制调用这些初始化例程的一种方法?

Ken

1 个解决方案

#1


3  

Static initialization with global variable is generally considered harmful. The order of initialization is implementation dependent. Also it doesn't work well with Obj-C compilers.

使用全局变量进行静态初始化通常被认为是有害的。初始化的顺序依赖于实现。它也不能很好地使用object - c编译器。

Use Construct On First Use Idiom instead.

首先使用构造来代替习惯用法。

See also this site: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.15

参见本网站:http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.15。

#1


3  

Static initialization with global variable is generally considered harmful. The order of initialization is implementation dependent. Also it doesn't work well with Obj-C compilers.

使用全局变量进行静态初始化通常被认为是有害的。初始化的顺序依赖于实现。它也不能很好地使用object - c编译器。

Use Construct On First Use Idiom instead.

首先使用构造来代替习惯用法。

See also this site: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.15

参见本网站:http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.15。