如果在構造函數中用到了 new之類的初始化操作,在析構函數調用了delete.
如果沒有禁用拷貝構造,複製賦值 那麼在進行
A a; A b= a;
操作時,
b 進行析構的時候,會把a 申請的內存給析構掉,會造成a 裏面的對象已經釋放,導致不可預計的結果。
class example
{
public:
example()
{
base_ptr_ = new char[256];
strcpy(base_ptr_,"lee");
}
void show()
{
printf("the name is %s\n",base_ptr_);
}
~example()
{
delete base_ptr_;
base_ptr_ = NULL;
}
private:
char* base_ptr_;
// example(const example&);
// example operator = (const example&);
};
int _tmain(int argc, _TCHAR* argv[])
{
example __ex1;
__ex1.show();
example __ex2 = __ex1;
return 0;
}
當 __ex2 進行析構的話,就會出現二次刪除的情況,base_ptr_ 已經在第一次的時候刪除了。