#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
总结以上是内存溢出为你收集整理的cocos2dx3.2 从引擎中学到的一招,创建新类,构造函数和虚析构函数都应该是保护类型全部内容,希望文章能够帮你解决cocos2dx3.2 从引擎中学到的一招,创建新类,构造函数和虚析构函数都应该是保护类型所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)