Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360171
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-22 08:54:58

  •  what is singleton class(and how will u write this)
Singleton
The term Singleton refers to an object that can only be instantiated once. This pattern is generally used where a global variable would have otherwise been used. The main advantage of the singleton is that its existence is guaranteed. Other advantages of the design pattern include the clarity, from the unique access, that the object used is not on the local stack. Some of the downfalls of the object include that, like a global variable, it can be hard to tell what chunk of code corrupted memory, when a bug is found, since everyone has access to it.

Let's take a look at how a Singleton differs from other variable types.
Like a global variable, the Singleton exists outside of the scope of any functions. Traditional implementation uses a static member function of the Singleton class, which will create a single instance of the Singleton class on the first call, and forever return that instance. The following code example illustrates the elements of a C++ singleton class, that simply stores a single string.

  1. class StringSingleton
  2. {
  3.         public:
  4.                 // Some accessor functions for the class, itself
  5.                 std::string GetString() const {return mString;}
  6.                 void SetString(const std::string &newStr) {mString = newStr;}

  7.                 // The magic function, which allows access to the class from anywhere
  8.                 // To get the value of the instance of the class, call:
  9.                 // StringSingleton::Instance().GetString();
  10.                 static StringSingleton &Instance()
  11.                 {
  12.                         // This line only runs once, thus creating the only instance in existence
  13.                         static StringSingleton *instance = new StringSingleton;
  14.                         // dereferencing the variable here, saves the caller from having to use
  15.                         // the arrow operator, and removes tempation to try and delete the
  16.                         // returned instance.
  17.                         return *instance; // always returns the same instance
  18.                 }

  19.         private:
  20.                 // We need to make some given functions private to finish the definition of the singleton
  21.                 StringSingleton(){} // default constructor available only to members or friends of this class

  22.                 // Note that the next two functions are not given bodies, thus any attempt
  23.                 // to call them implicitly will return as compiler errors. This prevents
  24.                 // accidental copying of the only instance of the class.
  25.                 StringSingleton(const StringSingleton &old); // disallow copy constructor
  26.                 const StringSingleton &operator=(const StringSingleton &old); //disallow assignment operator

  27.                 // Note that although this should be allowed,
  28.                 // some compilers may not implement private destructors
  29.                 // This prevents others from deleting our one single instance, which was otherwise created on the heap
  30.                 ~StringSingleton(){}
  31.         private: // private data for an instance of this class
  32.                 std::string mString;
  33. };





阅读(1007) | 评论(1) | 转发(0) |
0

上一篇:友元函数

下一篇:预处理

给主人留下些什么吧!~~

onezeroone2011-04-24 16:10:26

only be instantiated once, orever return that instance