Consider the following code
class Foo {
public:
static Foo *create() {
/*if (!_pInstance)
_pInstance = new Foo();*/
return _pInstance;
}
void print() {
cout << "Hi there "<<this << endl;
}
private:
static Foo *_pInstance;
Foo() = default;
//~Foo() = default;
};
Foo *Foo::_pInstance = nullptr;
int main()
{
Foo *obj1 = Foo::create();
obj1->print();
delete obj1;
obj1->print();
system("PAUSE");
return 0;
}
I was working on the singleton design pattern, but I faced by the static object's strange world.
This code output is:
Hi there 00000000
Hi there 00000000
Why? How I can delete this object? I wanna a solution to cause a crash in this program.