散分100!顺便问一下...

bluei 2003-08-03 03:47:25
熬了2天,终于长了两个三角。临走想谢谢大家,又不能滋长不劳而获的苗头,就顺便把刚看到的问题重提一下:
构造一个类singleton,使该类只能存在一个实例,请写出你的实现。
实现方法有多种,提出者皆有分。

bluei
...全文
26 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
galoit 2003-08-04
  • 打赏
  • 举报
回复
to season11(中漫步) and everyone above

我终于理解了 楼主的意思 又学了一招 呵呵

谢谢各位了 !!
fixopen 2003-08-04
  • 打赏
  • 举报
回复
C++语言本身就通过static支持单一实例。使用它就行了。
bluei 2003-08-04
  • 打赏
  • 举报
回复
按照我的理解,来总结一下。主要思路是把构造函数私有化,在给用户另外提供的创建接口里,禁止多个对象的同时存在。接口可以是public&static, 可以是友元。类定义一个静态的对象指针是必要的,依靠它来判断当前是否已经存在实例。singleton是一种成型的设计模式,看来很有必要读一读《设计模式》。下面散分:
------------------------------------------------------
galoit(虎虎):跟我一样,学习为主,谢谢支持:)1'
Jinhao(辣子鸡丁):这两天没少切磋,高手;5'
Dragon132(Dragon):谢谢捧场,有角同加;1'
MaiCle(【三星S308】):高手。12'
aflyinghorse():有钻劲儿,辛苦了。25'
eason11(云中漫步):只要进了构造函数,就来不及了。1'
magic007(无名):another高手。12'
yin0731(yin0731):希望计算机的感觉就是咱们的感觉。1'
hillyee(山之笛):等我三星了,我就一次1000分;现在心有余而级不足。1'
hlnpro(错误!死机?晕~~~):cls(const cls&){}怎么回事?:)12'
fixopen(dup):我是在说类实例呀!1'
yndfcd(YNDFCD):只要同一时间不存在多个实例就可以了吧。1'
minghui000(绿豆冰好好味):精华。12'
cgsw12345(cgsw):升华。12'
cityvagrant(城市过客) 、snowman_pc(cpp)、chinazcw(笑口常开) :谢谢支持。虽然帖子已经丑话说在前头了,但……1'×3
----------------------------------------------
cityvagrant 2003-08-04
  • 打赏
  • 举报
回复
呵呵,不知能否接到分。
bluei 2003-08-04
  • 打赏
  • 举报
回复
hlnpro:
首先感谢你的支持。程序稍加修改,即可通过编译。确实只创建一个实例。但有个疑惑的地方,请赐教一下:
cls(const cls&){}
'&'用在这里有什么作用?删掉这句,程序也照常运行,这句代码什么时候起作用?
chinazcw 2003-08-04
  • 打赏
  • 举报
回复
没有实现,纯粹是来混分的~~~~
cgsw12345 2003-08-04
  • 打赏
  • 举报
回复
class Printer {
public:
class TooManyObjects{};
// 伪构造函数
static Printer * makePrinter();
static Printer * makePrinter(const Printer& rhs);
...
private:
static size_t numObjects;
static const size_t maxObjects = 1; // 见下面解释
Printer();
Printer(const Printer& rhs);
};
// Obligatory definitions of class statics
size_t Printer::numObjects = 0;
const size_t Printer::maxObjects;
Printer::Printer()
{
if (numObjects >= maxObjects) {
throw TooManyObjects();
}
...
}
Printer::Printer(const Printer& rhs)
{
if (numObjects >= maxObjects) {
throw TooManyObjects();
}
...
}
Printer * Printer::makePrinter()
{ return new Printer; }
Printer * Printer::makePrinter(const Printer& rhs)
{ return new Printer(rhs); }
cgsw12345 2003-08-04
  • 打赏
  • 举报
回复
第一、类的构造函数是private。这样能阻止建立对象。
第二、使用友元访问类的静态对象,这意味着只有一个对象被建立。
class Printer {
public:
static Printer& thePrinter();
...
private:
Printer();
Printer(const Printer& rhs);
...
};
Printer& Printer::thePrinter()
{
static Printer p;
return p;
}
snowman_pc 2003-08-04
  • 打赏
  • 举报
回复
啊,

恭喜

可惜帮不上你!
minghui000 2003-08-04
  • 打赏
  • 举报
回复
class A
{
private:
static A * pa;
A(){};
public:
A * create()
{
if(pa) return pa
else new A();
};
};
A* A::pa = 0;
yndfcd 2003-08-04
  • 打赏
  • 举报
回复
难道不能这样。
CTest* pTest1 = CTest::Instance();
CTest::pTestSingleton = NULL;
CTest* pTest2 = CTest::Instance();
hlnpro 2003-08-04
  • 打赏
  • 举报
回复
class cls{
protected:
cls(){}
cls(const cls&){}
public:
friend cls *get_cls();
};
cls *get_cls(){
static p=new cls;
return p;
}
hillyee 2003-08-03
  • 打赏
  • 举报
回复
通常像这种100的问题,最后分给每一个人的就只有几分了,楼主考虑多放点分出来,ok ?
例如说500,1000之类的。
bluei 2003-08-03
  • 打赏
  • 举报
回复
太晚了,明天来总结、结帖。谢谢各位。
没有其他的思路了?
bluei 2003-08-03
  • 打赏
  • 举报
回复
呵呵,看/系统功能/论坛规则。
aflyinghorse 2003-08-03
  • 打赏
  • 举报
回复
bluei(蓝之我) : 我改了几次才改对的:)

magic007(无名):方法不错,学习

顺便问一下,怎样才能多几个三角,我就一个:(
yin0731 2003-08-03
  • 打赏
  • 举报
回复
感觉是这样啊
magic007 2003-08-03
  • 打赏
  • 举报
回复
我的实现并带有示例:

#include <iostream>
using namespace std;
class Singleton
{
private:
Singleton(){cout<<"Singleton constructor"<<endl;}
~Singleton() {cout<<"Singleton destructor"<<endl;}
Singleton(const Singleton&);
public:
static Singleton* GetInstance();
void DispInfo()
{
cout<<"My name is Singleton!"<<endl;
}
};
Singleton* Singleton::GetInstance()
{
static Singleton _singleton;
return &_singleton;
}

int main()
{
Singleton *mySingleton=Singleton::GetInstance();
mySingleton->DispInfo();
system("PAUSE");
return 0;
}
在这个示例中,singleton对象会自动析构。
bluei 2003-08-03
  • 打赏
  • 举报
回复
没有仔细看你的程序,原来:
if(pa)
return pa;
冤枉你了。呵呵
写的很好呀!学习
bluei 2003-08-03
  • 打赏
  • 举报
回复
aflyinghorse():
现在新添加一个成员函数f(),打印一条语句。然后不同实例调用f();应该只有第一个实例能够调用成功。可是……

#include <stdio.h>
class A
{
private:
static A * pa;
A(){};
public:
void f(); //添加f()声明------bluei
static A * create()
{
if(pa)
return pa;
else
{
// printf("here one time!");//注释掉---bluei
return (pa=new A());
}
}
static void destroy(){if(pa) delete pa;}
};
//添加函数定义三行----bluei
void A::f(){
printf("I'm here!\n");
}
A* A::pa = 0;
main()
{
A* pa = A::create();
A* pa1 = A::create();
A* pa2 = A::create();
//添加三行测试语句-----bluei
pa->f();
pa1->f();
pa2->f();
A::destroy();
getchar();
}

输出:
I'm here!
I'm here!
I'm here!

我提供的这种测试方法对不对?如果对,那就说明你的程序不妥。
加载更多回复(12)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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