为什么报这个错

quickSort 2013-06-05 10:45:02
代码:
#include <iostream>

using namespace std;
class Singleton
{
protected:
Singleton() {cout<<"con of Singleton"<<endl;}
~Singleton(){cout<<"dcon of Singleton"<<endl;}
//Singleton(const Singleton& s);///只定义,不实现

public:
static Singleton * getInstance()
{
if(_instance==0)
{
_instance = new Singleton();
}
return _instance;
}
///other class functions...
void show()
{
cout<<"This is a Singleton !"<<endl;
}

private:
static Singleton * _instance;
///other class members ...

class Proxy
{
public:
~Proxy()
{
if(_instance!=0)
{
delete _instance;
_instance = 0;
}
}
};
static Proxy pt;
};
///静态成员的初始化
Singleton* Singleton::_instance = 0;//new Singleton();
Singleton::Proxy Singleton::pt;

int main()
{
Singleton *s=Singleton::getInstance();
s->show();
Singleton *a=Singleton::getInstance();
a->show();
cout<<(a == s)<<endl;
Singleton c(*s);///compile error: 'Singlenton::~Singlenton() is protected'
c.show();
return 0;
}


我想实现一个单例模式,写了上面的代码。
问题是:
Singleton c(*s);///compile error: 'Singlenton::~Singlenton() is protected'
这里为什么报错'Singlenton::~Singlenton() is protected?
我理解的是这里应该调用复制构造函数,我注释掉了,所以编译器应该合成一个,
然后生成了一个instance 。
然后在main运行完之后析构的时候报错Singlenton::~Singlenton() is protected'才可以理解。
...全文
105 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
大尾巴猫 2013-06-06
  • 打赏
  • 举报
回复
析构函数要放在public下面。 只看过构造函数放在私有和保护下,不让随便构造。没看过不让析构的。
  • 打赏
  • 举报
回复
堆栈变量的析构是在return之前插入的,但是你觉得编译器在哪个位置提示你友好一点呢?难道在return那里?这个是编译器人性化的表现
xiaohutushen30 2013-06-06
  • 打赏
  • 举报
回复
就算只有一个对象最后也是要释放的!
AndyStevens 2013-06-06
  • 打赏
  • 举报
回复
引用 4 楼 hello_world_2012 的回复:
[quote=引用 2 楼 mougaidong 的回复:] 此处会调用析构函数,因为main函数内的自动变量c是要回收的,编译器会自动插入调用析构函数的语句。 [quote=引用 1 楼 hello_world_2012 的回复:] 难道是这里调用了析构函数??? Singleton c(*s);///compile error: 'Singlenton::~Singlenton() is protected'
[/quote] 嗯,这个我知道。 但是自动调用析构函数应该在return 之后吧, 至少也要在c.show();之后吧?? [/quote]你这是编译错误,跟运行在c.show();之后有什么关系,编译器看到你有个局部对象,就自动添加了析构代码,然后发现你的析构居然是个保护类型,所以反馈错误,你要防止别人随便析构,这里的别人也包括了编译器。
quickSort 2013-06-06
  • 打赏
  • 举报
回复
引用 3 楼 ananluowei 的回复:
析构函数要放在public下面。 只看过构造函数放在私有和保护下,不让随便构造。没看过不让析构的。
这里的目的就是不让用户随意析构。
quickSort 2013-06-06
  • 打赏
  • 举报
回复
引用 2 楼 mougaidong 的回复:
此处会调用析构函数,因为main函数内的自动变量c是要回收的,编译器会自动插入调用析构函数的语句。 [quote=引用 1 楼 hello_world_2012 的回复:] 难道是这里调用了析构函数??? Singleton c(*s);///compile error: 'Singlenton::~Singlenton() is protected'
[/quote] 嗯,这个我知道。 但是自动调用析构函数应该在return 之后吧, 至少也要在c.show();之后吧??
turing-complete 2013-06-05
  • 打赏
  • 举报
回复
此处会调用析构函数,因为main函数内的自动变量c是要回收的,编译器会自动插入调用析构函数的语句。
引用 1 楼 hello_world_2012 的回复:
难道是这里调用了析构函数??? Singleton c(*s);///compile error: 'Singlenton::~Singlenton() is protected'
quickSort 2013-06-05
  • 打赏
  • 举报
回复
难道是这里调用了析构函数??? Singleton c(*s);///compile error: 'Singlenton::~Singlenton() is protected'

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧