C++ 中的单例类
我按照以下示例使用了单例类:
但我得到的错误为 “无法解析的外部符号“
这是我尝试过的代码:
#include<iostream>
using namespace std;
class singleton
{
int value;
static singleton *instance;
protected:
singleton()
{
value=0;
}
public:
static void initialize()
{
if(instance==NULL)
singleton();
else
cout<<"An instance of singleton already exist...";
}
static singleton& getInstance()
{
return *instance;
}
int getValue()
{
return value;
}
};
void main()
{
singleton::initialize();
}
对 Singleton 类进行一点解释就太好了。它使用的场景。优点和缺点。单例的替代品。等等
I have used singleton calss following the example:
But i get the error as "Unresolved external symbols"
this is the code i tried out:
#include<iostream>
using namespace std;
class singleton
{
int value;
static singleton *instance;
protected:
singleton()
{
value=0;
}
public:
static void initialize()
{
if(instance==NULL)
singleton();
else
cout<<"An instance of singleton already exist...";
}
static singleton& getInstance()
{
return *instance;
}
int getValue()
{
return value;
}
};
void main()
{
singleton::initialize();
}
A little bit explanation on Singleton classes would be really great. The scenario its used. advantages and drawbacks. Alternatives to Singleton. etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我认为:
应该是:
按照您的方式,您实际上并没有存储新实例化的对象,因此
instance
将始终为空。使用以下方式显式设置静态也是一种很好的形式:(
在类定义之外)。
事实上,最好从基线单例代码开始,然后从那里开始逐步完善。这是标准形式的指针版本:
您可以从输出中看到两个指针是相同的:
或参考版本,因为这似乎是您的目标:
For a start, I think:
should be:
The way you have it, you're not actually storing the newly instantiated object so
instance
will always be null.It's also good form to explicitly set statics with:
(outside the class definition).
In fact, it's possibly better to start with the baseline singleton code and work your way up from there. This is a standard-form pointer version:
You can see that both pointers are the same from the output:
Or the reference version, since that seems to be what you're aiming for:
在定义文件中,您需要实例的定义:
如果您想在多个翻译单元中使用单例,则应该将定义与声明分开。
另外,通常的做法是没有初始化方法:
关于单例是好还是坏有很多讨论,普遍的共识是应该避免它们。您还应该检查一下它们。
In your definition file, you need the definition of
instance
:You should separate your definition from your declaration if you want to use the singleton in multiple translation units.
Also, the way it's usually done, is not having an initialize method:
There are lots of discussions on SO whether singletons are good or bad, with the general consensus that they should be avoided. You should also check them out.
您需要定义静态成员变量
instance
。在全局范围内的某个位置(例如在类和main
函数之间)添加以下行:此外,您可以将实例的初始化放在
getInstance
中:并删除
初始化
函数。您可能还希望将构造函数设为私有而不是受保护,并使用私有复制构造函数和赋值函数来防止复制和赋值。
You need to define the static member variable
instance
. Somewhere in global scope (for example between the class and themain
function) add the following line:Also, you can put the initialization of the instance in
getInstance
:and remove the
initialize
function.You might also want to make the constructor private instead of protected, as well as having private copy-constructor and assignment functions to prevent copying and assignment.