当前位置: 技术问答>linux和unix
请看我的析构函数释放内存有错误么?
来源: 互联网 发布时间:2015-11-15
本文导语: 就是下面的这种情况 #include class son; class father { public: int i; son* p_m_son; father() { i = 0; } virtual void output() { printf("this is fathern"); } ~father() { delete p_m_son; } }; class son : public father { public: void output() {...
就是下面的这种情况
#include
class son;
class father
{
public:
int i;
son* p_m_son;
father()
{
i = 0;
}
virtual void output()
{
printf("this is fathern");
}
~father()
{
delete p_m_son;
}
};
class son : public father
{
public:
void output()
{
father::output();
printf("this is sonn");
}
};
int
main ()
{
son s;
s.output();
printf("i:%dn", s.i);
return 1;
}
在cygwin下面
#include
class son;
class father
{
public:
int i;
son* p_m_son;
father()
{
i = 0;
}
virtual void output()
{
printf("this is fathern");
}
~father()
{
delete p_m_son;
}
};
class son : public father
{
public:
void output()
{
father::output();
printf("this is sonn");
}
};
int
main ()
{
son s;
s.output();
printf("i:%dn", s.i);
return 1;
}
在cygwin下面
|
析构~father()
{
delete p_m_son;
}
用到了son的定义,但前边只有声明。
在father的构造里要p_m_son=NULL
father的析构必须声明为virtual,否则多态析构会有问题。
{
delete p_m_son;
}
用到了son的定义,但前边只有声明。
在father的构造里要p_m_son=NULL
father的析构必须声明为virtual,否则多态析构会有问题。