请高手指点啊——设计模式之单例模式(C++代码实现)

hisense2423 2008-02-18 08:18:09
设计模式之单例模式

各位,
我用C++写了设计模式中的单例模式,但是链接时有点问题,还请高手多多指教啊,多谢了。

#include <iostream>

using namespace std;

class Singleton
{
private:
static Singleton *instance;

Singleton()
{
}

public:

static Singleton *GetInstance()
{
if (NULL == instance)
{
instance = new Singleton();
}
return instance;
}
};

int main()
{
Singleton *p1 = Singleton::GetInstance();
Singleton *p2 = Singleton::GetInstance();

if (p1 == p2)
{
cout<<"Two objects is the same instance"<<endl;
}

return 1;
}

在VC++6.0下链接时显示错误:

--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
Linking...
test.obj : error LNK2001: unresolved external symbol "private: static class Singleton * Singleton::instance" (?instance@Singleton@@0PAV1@A)
test___Win32_Debug/test.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

test.exe - 2 error(s), 0 warning(s)



...全文
6819 40 打赏 收藏 转发到动态 举报
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
tlby1990 2012-08-23
  • 打赏
  • 举报
回复
收藏了!受益匪浅呀!
dreamgis 2012-05-24
  • 打赏
  • 举报
回复
我也mark一下,,貌似找不到没有完美的版本哦
xiaojinmao 2012-04-21
  • 打赏
  • 举报
回复
都是牛人,mark
LittleNumb 2012-04-17
  • 打赏
  • 举报
回复
标记一下!!!
CG 2012-04-14
  • 打赏
  • 举报
回复
四年前的老帖,小辈来瞻仰一下
Yanger_xy 2011-11-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jixingzhong 的回复:]

static Singleton *instance;

对它在类外部初始化一下
[/Quote]

正解
hackbuteer1 2011-10-28
  • 打赏
  • 举报
回复
学习了
renwoxingshen 2011-08-22
  • 打赏
  • 举报
回复
我测试过的,你可以参考一下。
class SimpleObject
{
private:
SimpleObject()
{
message = new char[30];

memset(message,0,30);
}
public:
~SimpleObject()
{
}

static SimpleObject& GetSimpleOjbct()
{
static SimpleObject m_object;

return m_object;
}

void SetSimpleObjectX(int n);
void SetSimpleOjbectY(int n);
void SetSimpleOjbectZ(int n);
void SetSimpleObjectMessage(char* msg);
void ShowObject();
private:
int x;
int y;
int z;
char* message;
};

void SimpleObject::SetSimpleObjectX(int n)
{
x = n;
}

void SimpleObject::SetSimpleObjectY(int n)
{
y = n;
}

void SimpleObject::SetSimpleObjectZ(int n)
{
z = n;
}

void SimpleObject::SetSimpleObjectMessage(char* msg)
{
strcpy(message,msg);
}

void ShowObject()
{
cout<<x<<":"<<y<<":"<<z;

cout<<message<<endl;
}

int main(int agrc,char* argv[])
{
SimpleOjbect A = SimpleObject::GetSimpleObject();

A.SetSimpleObjectX(0);
A.SetSimpleObjectY(0);
A.SetSimpleObjectZ(0);
A.SetSimpleObjectMessage("this is SimpleObject A");

A.ShowObject();

SimpleObject B = SimpleObject::GetSimpleObject();

B.SetSimpleOjbectX(12);

B.ShowObject();

return 0;
}
jody_go 2009-06-08
  • 打赏
  • 举报
回复
这个貌似有点问题,
好像会多析构一次啊·
P_ghost 2008-11-18
  • 打赏
  • 举报
回复
私有静态成员!!!LZ真行!
bfhtian 2008-11-18
  • 打赏
  • 举报
回复
这帖不用结的吗
once_and_again 2008-11-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Jim_King_2000 的回复:]
兄弟,singleton是不用new的。如果用了new,在什么地方去delete它呢?singleton原理的基础是局部static变量。
C/C++ codeclassSingleton
{private:staticSingleton*instance;

Singleton()
{
}public:staticSingleton*GetInstance()
{staticSingleton singleton;return&singleton
}
};
这种局部static变量只有在函数被调用的第一次才被创建,随着程序的结束而结束…
[/Quote]private 保证只有一个实现,
static data + static member function !=singleton,
不容易销毁,那本书叫做 不死的凤凰
flydream1980 2008-11-17
  • 打赏
  • 举报
回复
呵呵,受教了。高手比较多。
apunix 2008-04-06
  • 打赏
  • 举报
回复
mark,学习
youngshuaishuai 2008-03-10
  • 打赏
  • 举报
回复
mark
Oversense 2008-02-22
  • 打赏
  • 举报
回复
class Singleton
{
public:

static Singleton &GetInstance()
{
Singleton instance
return instance;
}
};

我喜欢这样,多线程更多的是靠设计上去避免
chengzhe 2008-02-22
  • 打赏
  • 举报
回复
Singleton* Singleton::instance = NULL;
rularys 2008-02-22
  • 打赏
  • 举报
回复
我想这里的 Singleton 是针对语言本身提出的解决方案;如果将之与系统相关的设计联系起来,未免有些牵强了。
当然实际应用的时候首先考虑的是系统的设计,然后才是具体的解决方法,而这里的Singleton ,正是在C++中的
解决方法之一。和系统相关的设计应该被分离出去。设计首先要保证 Singleton 的应用是系统安全的,不管是
在何种环境下,这个条件成立以后,才会提到它的具体实现的设计——比如说这里的C++ Singleton 。否则,会把
很多问题缠绕在一起,有时候根本不能解决问题
czp_opensource 2008-02-22
  • 打赏
  • 举报
回复
static Singleton *instance;
zenny_chen 2008-02-21
  • 打赏
  • 举报
回复
To Jim_King_2000 :

实际上在你的代码中,由于使用了static,尽管在函数内,但是在加载器加载你的应用程序时仍然会为你的全局唯一的Singleton预留空间。只不过在调用GetInstance()时才调用构造函数(若有的话)。
而我的方法是在加载应用程序后,在初始化代码中调用构造函数(如果有的话),但却带来了额外的安全性。实际上你所谓的存储空间效率两者是一样的,而且结束生命的时间也一样。除非你在构造函数中调用了内存动态分配函数,这样很显然,我的代码将在初始化过程中分配空间;而你的将在第一次调用GetInstance()时分配。
加载更多回复(19)

64,662

社区成员

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

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