私はこの振る舞いを理解することができません。私はクラスAを持っています、
class A
{
public:
int ch;
char *s;
A()
{}
A(char *st):ch(0)
{
s = new char[10];
strcpy(s,st);
}
A(const A& rhs):ch(rhs.ch)
{
s = new char[strlen(rhs.s)+1];
strcpy(s,rhs.s);
}
const A& operator=(const A& rhs)
{
char *temp = new char[strlen(rhs.s)+1];
strcpy(temp,rhs.s);
delete[] s;
s=temp;
ch = rhs.ch;
return *this;
}
~A()
{
delete []s;
}
};
この時点まで、すべてが期待どおりに進み、コピーコンストラクターと代入演算子をテストでき、それらは正しく機能しています。
子クラスBを作成しましたが、ヒープ破損エラーが発生します。理解できませんが、これはクラスAのデストラクタに関連する問題ですか。?以下は私のクラスBです。
class B:public A
{
public:
int a;
B():a(0){}
};