构造对象c ++(cocos2d-x)时的差异

问题描述 投票:0回答:1

通过以下方法构造对象有什么区别?

auto scene = Scene::createWithPhysics();

而不是:

Scene* scene;
scene->createWithPhysics();
c++ cocos2d-x
1个回答
0
投票
auto scene = Scene::createWithPhysics();

在第一个示例中,您调用静态方法createWithPhysics,该方法为新的Scene对象分配内存,对其进行一些基本设置并返回其指针。

Scene* scene;
scene->createWithPhysics();

在第二个示例中,您创建了指向Scene对象的空指针,然后在其上调用一些方法。它应该引发异常,因为poiner尚未初始化(指向无处)。

© www.soinside.com 2019 - 2024. All rights reserved.