我需要在这个类的析构函数中写任何东西吗?
谢谢大家!现在我改变了我的逻辑。因为如果我包含指向自身的相同指针,它将创建无限循环。那么对于这个修改后的版本,我需要编写析构函数吗?
#include <stdio.h>
#include <stdlib.h>
#include <tr1/array>
using namespace std;
class Graphnode {
public:
std::tr1::array<int, 16> state;
int x;
int depth;
Graphnode(std::tr1::array<int, 16>,int,int);
Graphnode();
//~Graphnode();
};
Graphnode::Graphnode()
{
int i=0;
for(i=0;i<16;i++)
{
state[i] = 0;
}
x = 0;
depth = 0;
}
Graphnode::Graphnode(std::tr1::array<int, 16> _state,int _x,int _d)
{
int i=0;
for(i=0;i<16;i++)
{
state[i] = _state[i];
}
x = _x;
depth = _d;
}
/*Graphnode::~Graphnode()
{
}*/
Thanks for everybody! Now I changed my logic. Since if I contain the same pointer point to itself it will create infinite loop. So for this revised one do I need to write the destructor?
#include <stdio.h>
#include <stdlib.h>
#include <tr1/array>
using namespace std;
class Graphnode {
public:
std::tr1::array<int, 16> state;
int x;
int depth;
Graphnode(std::tr1::array<int, 16>,int,int);
Graphnode();
//~Graphnode();
};
Graphnode::Graphnode()
{
int i=0;
for(i=0;i<16;i++)
{
state[i] = 0;
}
x = 0;
depth = 0;
}
Graphnode::Graphnode(std::tr1::array<int, 16> _state,int _x,int _d)
{
int i=0;
for(i=0;i<16;i++)
{
state[i] = _state[i];
}
x = _x;
depth = _d;
}
/*Graphnode::~Graphnode()
{
}*/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您尝试创建一个图形节点时,您的代码将创建一个无限循环
您可以尝试为子图形节点创建一个特定的构造函数,以便在该构造函数中不会创建更多图形节点
Your code will create an infinite loop when you try to create one Graphnode
You could try creating a specific constructor for the sub-Graphnodes so that within that constructor more Graphnode's are not created