我对C++编程不是很熟悉.我知道编程的基础知识(语法,指针等),我用它构建了一些基本程序,并在工作中完成了一些基本的调试.我对Box2D的这行代码感到困惑,特别是来自Cocos2D的Box2dTest项目:
// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0); // bottom-left corner
Run Code Online (Sandbox Code Playgroud)
如果没有初始化,groundBodyDef怎么能做到这一点呢?我知道这不是Objective-C的事情,因为Box2D本身的C++示例就像这样.
小智 7
groundBodyDef 实际上已初始化!
我认为你期望的东西是这样的:
b2BodyDef *groundBodyDef = new b2BodyDef();
Run Code Online (Sandbox Code Playgroud)
它实际上仍然有效,但它在堆上初始化.在您的版本中,groundBodyDef在堆栈上初始化,就像您int在堆栈上初始化一样.
在没有参数的情况下调用它时,使用默认构造函数.