关于析构函数的一点问题
廖根 2006-12-28 02:18:37 #include<iostream>
#include <string>
using namespace std;
class CBase
{
public:
CBase()
{
m_base=new char[5];
m_base="aaa";
record=0;
}
void setRecord(int i)
{
record=i;
}
CBase(const CBase &Cb)
{
if (&Cb==NULL)
{
return;
}
m_base=new char[5];
strcpy(m_base,Cb.m_base);
record=Cb.record;
}
CBase operator=(const CBase & Cb)
{
if (this==&Cb)
{
return *this;
}
delete[] m_base;
m_base=new char[5];
strcpy(m_base,Cb.m_base);
record=Cb.record;
return *this;
}
~CBase()
{
cout<<record;
delete []m_base;
}
private:
char *m_base;
int record;
};
void test()
{
CBase Test;
Test.setRecord(1);
CBase Test2(Test);
}
void main()
{
test();
int testin=0;
}
这个小程序为什么第2次运行到delete []m_base就会出错?要是不用cout<<record和printf,就不会出错,为什么?