- 524 名前:デフォルトの名無しさん mailto:sage [2023/10/02(月) 22:07:14.88 ID:l4Vvzubd.net]
- 次のプログラムを実行するとコメントのように出力がされ、mainの最後で例外が発生してしまいます。
なぜ例外が発生するかわかる方教えてください。 mainを通してnewは2回実行され、deleteも2回実行されるので空ポインタをdeleteしていることもないと思うのです。 #include <iostream> using namespace std; class Csmp1 { private: int x; int* p; public: Csmp1(int n); ~Csmp1(); void disp() { cout << "x=" << x << endl;} }; Csmp1::Csmp1(int n) { x = n; p = new int; cout << "constructor " << x << endl; } Csmp1::~Csmp1() { delete p; cout << "destructor " << x << endl; } int main() { Csmp1 d3(300); // "constructor 300"と出力 d3.disp(); // "x=300"と出力 d3 = Csmp1(400); // "constructor 400"と出力し、その後"destructor 400"と出力 d3.disp(); // x=400と出力 return 0; } // デストラクタが呼ばれ、delete の部分で"Unknowin signal"例外発生
|

|