加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx3.2 从引擎中学到的一招,创建新类,构造函数和虚析构函

发布时间:2020-12-14 19:58:14 所属栏目:百科 来源:网络整理
导读:#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

#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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读