为什么报这个错
代码:
我想实现一个单例模式,写了上面的代码。
问题是:
Singleton c(*s);///compile error: 'Singlenton::~Singlenton() is protected'
这里为什么报错'Singlenton::~Singlenton() is protected?
我理解的是这里应该调用复制构造函数,我注释掉了,所以编译器应该合成一个,
然后生成了一个instance 。
然后在main运行完之后析构的时候报错Singlenton::~Singlenton() is protected'才可以理解。
#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'才可以理解。
...全文
相关推荐
报error LNK2001 unresolved external symbol错就是因为你...
visual c++报error LNK2001 unresolved external symbol错就是因为你...
lombok 最新插件版本 idea更新后 lombok报红错
idea更新后 lombok报红错,提供lombok最新版本插件,以解决idea2018.3问题
lombok idea 插件最新版(解决报红错问题).zip
软件介绍: lombok-plugin-0.23.jar最新版本lombok idea 插件,以解决idea2018.3 idea更新后lombok报红错问题。
yum报11错信息error以及解决方案.docx
yum报11错信息error以及解决方案.docx
yum报11错信息error以及解决方案.pdf
yum报11错信息error以及解决方案.pdf


