考虑下面的类声明
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类中的数据成员?
发布于 2015-03-19 20:06:00
因此,在执行PhysicsMaterial构造函数时,_test数据成员发生了更改:
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成员的值的代码,这表明早期的构建有问题。
一个干净的、随后的完全重建解决了这个问题。
https://stackoverflow.com/questions/29140145
复制相似问题