#include <iostream>
using namespace std;
class singleton_p
{
static singleton_p *instance;
singleton_p() {};
//copy constructor
singleton_p(const singleton_p & b);
singleton_p& operator= (const singleton_p & A);
public:
static singleton_p & create_instance()
{
if(!instance)
{
instance=new singleton_p();
}
return *instance;
}
void display()
{
std::cout<<"singleton class display func called\n";
cout<<(this)<<endl;
cout<<"---------------------------------"<<endl;
}
};
singleton_p* singleton_p::instance = NULL;
int _tmain(int argc, _TCHAR* argv[])
{
singleton_p p1=singleton_p::create_instance();
p1.display();
// creating instance
singleton_p p2=singleton_p::create_instance();
p2.display();
return 0;
}
我还将复制构造函数和赋值运算符设为私有,以避免使用它们创建新实例。 我在这里定义单身人士课程。但它在
中给了我错误singleton_p p1 = singleton_p :: create_instance();
singleton_p p2 = singleton_p :: create_instance();
请在我做错的地方帮助我。 感谢