cocos2dx3.2 从引擎中学到的一招,创建新类,构造函数和虚析构函数都应该是保护类型

#include <iostream>

#include <vector>

using namespace std;

class Node

{

public:

static Node* create();

void autorelease();

protected:

Node();

virtual bool init();

virtual ~Node();

};


void Node::autorelease()

{

delete this;

}

Node* Node::create()

{

auto sp = new Node();

if(sp && sp->init())

{

}

else

{

delete sp;

}

return sp;

}


Node::Node()

{

}

Node::~Node()

{

cout << "the Node is destructed" << endl;

}


bool Node::init()

{

return true;

}

class Player : public Node

{

public:

static Player* create();

protected:

Player();

virtual ~Player();

virtual bool init();

};


Player::Player()

{

}

Player::~Player()

{

cout << "the player is destructed" << endl;

}



Player* Player::create()

{

auto sp = new Player();

if(sp && sp->init())

{

}

else

{

delete sp;

}

return sp;

}


bool Player::init()

{

return true;

}


int main()

{

auto sp = Player::create();

sp->autorelease();

return 0;

}

结果输出

the player is destructed

the Node is destructed

Program ended with exit code: 0

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...