Cocos2d-x AppDelegate数据成员初始化丢失
Cocos2d-x AppDelegate data member initialization lost
考虑以下类声明
class AppDelegate : private cocos2d::Application
{
private:
int _test;
// ....
};
我正在将数据成员int _test
初始化为
AppDelegate::AppDelegate() : _test(20) {
cocos2d::log("Initialzing %p with _test = %d", this, _test);
}
后来,当代码
bool AppDelegate::applicationDidFinishLaunching() {
cocos2d::log("Checking %p, found _test = %d", this, _test);
// ....
}
执行,输出为
Initialzing 0x10da4aa20 with _test = 20
// ....
Checking 0x10da4aa20, found _test = 1056964608
这表示_test
未初始化。这个问题似乎是cocos2d-x体系结构特有的,因为我无法在沙箱项目中重现这个问题(其中AppDelegate
被交换为一个更简单的类)。
我的问题是:为什么这个初始化丢失了?是否有其他方法可以初始化和使用AppDelegate
类中的数据成员?
因此,_test
数据成员在执行PhysicsMaterial
构造函数时发生了更改:
typedef struct CC_DLL PhysicsMaterial
{
float density; ///< The density of the object.
float restitution; ///< The bounciness of the physics body.
float friction; ///< The roughness of the surface of a shape.
PhysicsMaterial()
: density(0.0f)
, restitution(0.0f)
, friction(0.0f)
{}
PhysicsMaterial(float aDensity, float aRestitution, float aFriction)
: density(aDensity)
, restitution(aRestitution)
, friction(aFriction)
{} // Watch reveals that this was the culprit ...
}PhysicsMaterial;
显然,这个构造函数中没有任何代码应该更改_test
成员的值,这表明早期的构建有问题。
一次彻底的重建和随后的全面重建解决了这个问题。
相关文章:
- 是否可以使用右值初始化数据成员?
- 扩展初始值设定项列表和未初始化的成员
- 初始化类成员的更好做法是什么?
- 使用 {} 初始化类成员
- 构造函数正在初始化数据成员
- 在类声明中初始化 const 成员变量时在调试模式下出现异常
- 初始化 const 成员的正确方法
- 初始化数据成员取决于构造函数中的条件
- 获取未初始化对象成员的地址是否定义良好?
- 为什么C++可以使用未初始化的成员变量(引用或指针 *NOT 值复制*)来初始化其父类的成员变量
- C - 初始化静态成员的方法
- 使用大型数据集初始化类成员向量的最有效方法
- 如何在C 11中正确初始化数据成员
- 在C++中,如果我们不在构造函数中初始化数据成员,那么类中数据成员的值会是多少
- 为什么我们需要构造函数 C++,我们可以通过函数(初始化数据或访问私有数据成员)来做同样的事情
- 正在初始化数据文件中的静态常量成员
- 用成员函数初始化数据成员
- 如何用该类的其他成员数据初始化类成员数据
- 尝试在构造函数中初始化数据成员,但失败.为什么
- 在c++中使用初始化列表初始化数据成员引用