c++ - 如何删除静态对象
问题描述
考虑以下代码
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;
}
我正在研究单例设计模式,但我面临着静态对象的奇怪世界。
此代码输出为:
Hi there 00000000
Hi there 00000000
为什么?我怎样才能删除这个对象?我想要一个导致该程序崩溃的解决方案。
解决方案
推荐阅读
- python - 如何处理具有多个 json 对象的文件并在文件中添加新的 json 对象?
- javascript - 在 jquery 中点击后退按钮后删除的内容被替换回来
- azure - Azure DevOps 发布管道的工件位置
- python - 将 json 源中的数据抓取到 csv 中
- java - SimpleXML Element & Inline Elementlist 随机顺序
- angular - 如何在Angular中有条件地设置动画:进入和:离开过渡?
- java - 将 JPanel 添加到 JFrame 会更改其大小
- angular - 如何在浏览器中快速加载 Angular 6 应用程序?
- xamarin - 我可以使用 XAML 和 CS 模板覆盖控件属性以创建另一个控件吗?
- sql-server - Sql - SQL 错误 [156] [S1000]:关键字“as”附近的语法不正确