C++析构成员失败
Hi,
跟踪代码,t1和t2的地址均不相同,各自的m_name的地址也不相同,但是在析构时系统报错:
_BLOCK_TYPE_IS_VALID,分不多,尽量给。
代码如下:
#include <iostream>
#include <string>
#include <list>
using namespace std;
class point
{
public:
point(int x,int y,char* name):m_x(x),m_y(y),m_name(name)
{
}
point(const point& p)
{
if (&p != NULL)
{
*this = p;
}
}
point& operator= (const point& instance)
{
if(this == &instance)
{
return *this;
}
this->m_x = 12211112;
this->m_y = 21212;
int size = strnlen(instance.m_name,100);
size = size > 0 ? size : 1;
this->m_name = new char[size+1];
strcpy(this->m_name,instance.m_name);
return *this;
}
virtual ~point()
{
if(NULL != m_name)
{
delete[] m_name;//当析构时,第二次delete报错。
m_name= 0;
}
}
public:
virtual float square() =0;
protected:
int m_x;
int m_y;
char* m_name;
};
class traple : public point
{
public:
traple(int x,int y,int z,char* name):point(x,y,name),m_z(z){}
public:
virtual float square()
{
return this->m_x * this->m_y * this->m_z;
}
char* GetName() const
{
return this->m_name;
}
protected:
int m_z ;
};
void main()
{
char str[10];
memset(str,0,sizeof(str));
strcpy(str,"B11111");
traple t1(1,2,78,str);
traple t3 = t1;
}