首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cocos2d-x AppDelegate数据成员初始化丢失

Cocos2d-x AppDelegate数据成员初始化丢失
EN

Stack Overflow用户
提问于 2015-03-19 16:44:20
回答 1查看 111关注 0票数 0

考虑下面的类声明

代码语言:javascript
复制
class AppDelegate : private cocos2d::Application
{
private:
  int _test;
  // ....
};

我将数据成员int _test初始化为

代码语言:javascript
复制
AppDelegate::AppDelegate() : _test(20) { 
  cocos2d::log("Initialzing %p with _test = %d", this, _test);
}

稍后,当代码

代码语言:javascript
复制
bool AppDelegate::applicationDidFinishLaunching() {
  cocos2d::log("Checking %p, found _test = %d", this, _test);
  // ....
}

,则输出为

代码语言:javascript
复制
Initialzing 0x10da4aa20 with _test = 20
// ....
Checking 0x10da4aa20, found _test = 1056964608

这表示_test未初始化。这个问题似乎是cocos2d-x架构特有的,因为我不能在沙箱项目中重现这个问题(在沙箱项目中,AppDelegate被换成了一个更简单的类)。

我的问题是:为什么这个初始化丢失了?是否有其他方法可以初始化和使用AppDelegate类中的数据成员?

EN

回答 1

Stack Overflow用户

发布于 2015-03-19 20:06:00

因此,在执行PhysicsMaterial构造函数时,_test数据成员发生了更改:

代码语言:javascript
复制
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成员的值的代码,这表明早期的构建有问题。

一个干净的、随后的完全重建解决了这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29140145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档