私はこのコードにいくつかの疑問があります> 議論は物事を理解するのに非常に役立ちます:
class Singleton
{
private:
static Singleton *single;
Singleton() {}
~Singleton() {}
public:
static Singleton* getInstance()
{
if (!single)
single = new Singleton();
return single;
}
void method()
{
cout << "Method of the singleton class" << endl;
}
static void destroy()
{
delete single;
single = NULL;
}
};
Singleton* Singleton::single = NULL;
int main()
{
Singleton *sc2;
sc2 = Singleton::getInstance(); // sc2 is pointing to some memory location
{
Singleton *sc1 = Singleton::getInstance(); // sc1 and sc2 pointing to same memory location
sc1->method();
Singleton::destroy(); // memory location deleted.
cout << sc1;
}
sc2->method(); // ??? how this is working fine??
return 0;
}
このブロック内では、"Singleton::destroy()" でメモリを削除しています。
{
Singleton *sc1 = Singleton::getInstance();
sc1->method();
Singleton::destroy();
cout << sc1;
}
次に、これが「sc2->method();」を呼び出す方法 成功ですか??
デベシュ