-3

アプリケーションでは、リソースは、テクスチャやサウンドなど、アプリケーションによって使用される任意の仮想コンポーネントです。デストラクタでリソースをクリーンアップできますか? 可能であれば、デモンストレーションの例を提供してください。ありがとう。

デストラクタを使用してクリーンアップしようとしましたが、値はまだそこにあります。デストラクタを呼び出した後にオブジェクトが削除されないのはなぜですか?

#include <iostream>
using namespace std;

class Demo
{   
public:
Demo();       // Constructor prototype
~Demo();      // Destructor prototype

int a;
};

Demo::Demo()        // Constructor function definition
{   
cout << "An object has just been defined, so the constructor"
    << " is running.\n";
a = 1;
}

Demo::~Demo()       // Destructor function definition
{  
cout << "Now the destructor is running.\n";         
}

int main()
{   
cout << "This is displayed before the objects are created.\n";
Demo demoObjA, demoObjB;   // Define two Demo objects

cout << demoObjA.a << endl;
cout << "The objects now exist, but are about to be destroyed.\n";
demoObjA.~Demo();
cout << endl;

cout << demoObjA.a << endl;

cin.get();
return 0;   
}
4

2 に答える 2