Chinaunix首页 | 论坛 | 博客
  • 博客访问: 571257
  • 博文数量: 141
  • 博客积分: 3425
  • 博客等级: 中校
  • 技术积分: 1609
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-23 15:55
文章分类

全部博文(141)

文章存档

2019年(5)

2011年(19)

2010年(36)

2009年(13)

2008年(50)

2007年(18)

分类: C/C++

2011-08-29 11:22:41

  1. // Purpose. Singleton destroyer
  2.  //
  3.  // Discussion. Vlissides describes
  4.  // that Singletons can be cleaned-up
  5.  // by "wrapping" the ptr in a stack-
  6.  // based static member of another
  7.  // class whose sole responsibility is
  8.  // to have its destructor delete the
  9.  // Singleton's ptr. The Singleton
  10.  // destroyer is automatically cre-
  11.  // ated before main() is run, and
  12.  // initially contains a null ptr.
  13.  // The first time the inst() method
  14.  // is called, the destroyer is
  15.  // meaningfully initialized.
  16.                                          
  17.  #include <iostream>

  18.  using namespace std;
  19.  
  20.  class GlobalClass {
  21.  public:
  22.     int getValue() {
  23.        return value_; }
  24.     void setValue( int v ) {
  25.        value_ = v; }
  26.     static GlobalClass* inst() {
  27.        if ( ! globalObj_ )
  28.           globalObj_ = new GlobalClass;
  29.        return globalObj_; }
  30.  protected:
  31.     GlobalClass( int v=0 ) {
  32.        value_ = v; }
  33.     ~GlobalClass() { }
  34.  private:
  35.     int value_;
  36.     static GlobalClass* globalObj_;
  37.  };
  38.                                          
  39.  // Allocating and initializing
  40.  // GlobalClass's static data member
  41.  // (the ptr, not a GlobalClass inst)
  42.  GlobalClass*
  43.     GlobalClass::globalObj_ = 0;
  44.                                          
  45.  void foo( void ) {
  46.     GlobalClass::inst()->setValue( 1 );
  47.     cout << "foo: globalObj is " <<
  48.        GlobalClass::inst()->getValue()
  49.        << endl;
  50.  }
  51.  void bar( void ) {
  52.     GlobalClass::inst()->setValue( 2 );
  53.     cout << "bar: globalObj is " <<
  54.        GlobalClass::inst()->getValue()
  55.        << endl;
  56.  }
  57.  int main( void ) {
  58.     cout << "main: globalObj is " <<
  59.        GlobalClass::inst()->getValue()
  60.        << endl;
  61.     foo();
  62.     bar();
  63.     return 1;
  64.  }
  65.                                          
  66.  // main: globalObj is 0
  67.  // foo: globalObj is 1
  68.  // bar: globalObj is 2

另一个例子:

  1. // New design. "globalObj" is now a
  2. // static variable in the inst() ac-
  3. // cessor method. The single inst-
  4. // ance is enforced by declaring the
  5. // ctor non-public. [The dtor must
  6. // be public because of the static
  7. // variable instance.] Global
  8. // access is provided by the static
  9. // inst() method. The object is al-
  10. // located on first demand by C++,
  11. // and it is de-allocated automati-
  12. // cally by C++.

  13. #include <iostream>

  14. using namespace std;

  15. class GlobalClass {
  16. public:
  17.    int getValue() {
  18.       return value_; }
  19.    void setValue( int v ) {
  20.       value_ = v; }
  21.    static GlobalClass& inst() {
  22.       static GlobalClass globalObj;
  23.       return globalObj; }
  24.    ~GlobalClass() {
  25.       cout << ":dtor:" << endl; }
  26. protected:
  27.    GlobalClass( int v=0 ) {
  28.       cout << ":ctor: ";
  29.       value_ = v; }
  30. private:
  31.    int value_;
  32. };

  33. void foo( void )
  34. {
  35.    GlobalClass::inst().setValue( 1 );
  36.    cout << "foo: globalObj is " <<
  37.       GlobalClass::inst().getValue()
  38.       << endl;
  39. }

  40. void bar( void )
  41. {
  42.    GlobalClass::inst().setValue( 2 );
  43.    cout << "bar: globalObj is " <<
  44.       GlobalClass::inst().getValue()
  45.       << endl;
  46. }

  47. int main( void )
  48. {
  49.    cout << "main: globalObj is " <<
  50.       GlobalClass::inst().getValue()
  51.       << endl;
  52.    foo();
  53.    bar();
  54.    cout << "main: end" << endl;
  55.    return 1;
  56. }

  57. // main: globalObj is :ctor: 0
  58. // foo: globalObj is 1
  59. // bar: globalObj is 2
  60. // main: end
  61. // :dtor:

转自:







阅读(541) | 评论(0) | 转发(0) |
0

上一篇:C语言中的逗号表达式

下一篇:GIT备忘

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