LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2301|回复: 7

c++中构造函数为protected或private的用法?

[复制链接]
发表于 2003-11-23 01:29:06 | 显示全部楼层 |阅读模式
在看iostream library的时候,看到基类(i.e io_base)的构造函数是这样的。可是怎么样才能构造他(或者派生类)的对象呢?
从网上看到一些资料说,可以使用静态函数,友元函数,有没有其他的方法?因为我在他的头文件中,没有看到这些。
发表于 2003-11-23 19:59:18 | 显示全部楼层
给你一个实例,singleton模式

class singleton {
private:
  singleton() {}
  singleton(singleton&) {}

public:
  ~singleton() { _instance = NULL; }

  static singleton *instance() {
    if(_instance) {
      return _instance;
    } else {
      return _instance = new singleton();
    }
  }

private:
  static singleton *_instance;

  // Some data ...
};

singleton::singleton *_instance = NULL;
发表于 2003-11-23 20:02:24 | 显示全部楼层
表情符号!晕

  1. class singleton {
  2. private:
  3.   singleton() {}
  4.   singleton(singleton& ) {}

  5. public:
  6.   ~singleton() { _instance = NULL; }

  7.   static singleton *instance() {
  8.     if(_instance) {
  9.       return _instance;
  10.     } else {
  11.       return _instance = new singleton();
  12.     }
  13.   }

  14. private:
  15.   static singleton *_instance;

  16.   // Some data ...
  17. };

  18. singleton::singleton *_instance = NULL;
复制代码
 楼主| 发表于 2003-11-23 22:02:38 | 显示全部楼层

你这个是靠static成员函数实现的,看看ios_base的代码



  1.   // 27.4.2  Class ios_base
  2.   class ios_base
  3.   {
  4.   public:
  5.    
  6.     // 27.4.2.1.1  Class ios_base::failure
  7.     class failure : public exception
  8.     {
  9.     public:
  10. #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
  11.       //48.  Use of non-existent exception constructor
  12.       explicit
  13.       failure(const string& __str) throw();

  14.       // This declaration is not useless:
  15.       // [url]http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118[/url]
  16.       virtual
  17.       ~failure() throw();

  18.       virtual const char*
  19.       what() const throw();
  20.       
  21.     private:
  22.       enum { _M_bufsize = 256 };
  23.       char _M_name[_M_bufsize];
  24. #endif
  25.     };

  26.     // 27.4.2.1.2  Type ios_base::fmtflags
  27.     typedef _Ios_Fmtflags fmtflags;
  28.     // 27.4.2.1.2  Type fmtflags
  29.     static const fmtflags boolalpha =   fmtflags(__ios_flags::_S_boolalpha);
  30.     static const fmtflags dec =         fmtflags(__ios_flags::_S_dec);
  31.     static const fmtflags fixed =       fmtflags(__ios_flags::_S_fixed);
  32.     static const fmtflags hex =         fmtflags(__ios_flags::_S_hex);
  33.     static const fmtflags internal =    fmtflags(__ios_flags::_S_internal);
  34.     static const fmtflags left =        fmtflags(__ios_flags::_S_left);
  35.     static const fmtflags oct =         fmtflags(__ios_flags::_S_oct);
  36.     static const fmtflags right =       fmtflags(__ios_flags::_S_right);
  37.     static const fmtflags scientific =  fmtflags(__ios_flags::_S_scientific);
  38.     static const fmtflags showbase =    fmtflags(__ios_flags::_S_showbase);
  39.     static const fmtflags showpoint =   fmtflags(__ios_flags::_S_showpoint);
  40.     static const fmtflags showpos =     fmtflags(__ios_flags::_S_showpos);
  41.     static const fmtflags skipws =      fmtflags(__ios_flags::_S_skipws);
  42.     static const fmtflags unitbuf =     fmtflags(__ios_flags::_S_unitbuf);
  43.     static const fmtflags uppercase =   fmtflags(__ios_flags::_S_uppercase);
  44.     static const fmtflags adjustfield = fmtflags(__ios_flags::_S_adjustfield);
  45.     static const fmtflags basefield =   fmtflags(__ios_flags::_S_basefield);
  46.     static const fmtflags floatfield =  fmtflags(__ios_flags::_S_floatfield);

  47.     // 27.4.2.1.3  Type ios_base::iostate
  48.     typedef _Ios_Iostate iostate;
  49.     static const iostate badbit =          iostate(__ios_flags::_S_badbit);
  50.     static const iostate eofbit =          iostate(__ios_flags::_S_eofbit);
  51.     static const iostate failbit =         iostate(__ios_flags::_S_failbit);
  52.     static const iostate goodbit =         iostate(0);

  53.     // 27.4.2.1.4  Type openmode
  54.     typedef _Ios_Openmode openmode;
  55.     static const openmode app =            openmode(__ios_flags::_S_app);
  56.     static const openmode ate =            openmode(__ios_flags::_S_ate);
  57.     static const openmode binary =         openmode(__ios_flags::_S_bin);
  58.     static const openmode in =             openmode(__ios_flags::_S_in);
  59.     static const openmode out =            openmode(__ios_flags::_S_out);
  60.     static const openmode trunc =          openmode(__ios_flags::_S_trunc);

  61.     // 27.4.2.1.5  Type seekdir
  62.     typedef _Ios_Seekdir seekdir;
  63.     static const seekdir beg =                 seekdir(0);
  64.     static const seekdir cur =                 seekdir(SEEK_CUR);
  65.     static const seekdir end =                 seekdir(SEEK_END);

  66. #ifdef _GLIBCPP_DEPRECATED
  67.     typedef int io_state;
  68.     typedef int open_mode;
  69.     typedef int seek_dir;
  70. #endif

  71.     // Callbacks;
  72.     enum event
  73.     {
  74.       erase_event,
  75.       imbue_event,
  76.       copyfmt_event
  77.     };

  78.     typedef void (*event_callback) (event, ios_base&, int);

  79.     void
  80.     register_callback(event_callback __fn, int __index);

  81.   protected:
  82.     // Data Members
  83.     streamsize                 _M_precision;
  84.     streamsize                 _M_width;
  85.     fmtflags                 _M_flags;
  86.     iostate                 _M_exception;
  87.     iostate                        _M_streambuf_state;

  88.     // 27.4.2.6  Members for callbacks
  89.     // 27.4.2.6  ios_base callbacks
  90.     struct _Callback_list
  91.     {
  92.       // Data Members
  93.       _Callback_list*                 _M_next;
  94.       ios_base::event_callback         _M_fn;
  95.       int                         _M_index;
  96.       _Atomic_word                _M_refcount;  // 0 means one reference.
  97.    
  98.       _Callback_list(ios_base::event_callback __fn, int __index,
  99.                      _Callback_list* __cb)
  100.       : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
  101.       
  102.       void
  103.       _M_add_reference() { __atomic_add(&_M_refcount, 1); }

  104.       // 0 => OK to delete.
  105.       int
  106.       _M_remove_reference() { return __exchange_and_add(&_M_refcount, -1); }
  107.     };

  108.      _Callback_list*          _M_callbacks;

  109.     void
  110.     _M_call_callbacks(event __ev) throw();

  111.     void
  112.     _M_dispose_callbacks(void);

  113.     // 27.4.2.5  Members for iword/pword storage
  114.     struct _Words
  115.     {
  116.       void*         _M_pword;
  117.       long         _M_iword;
  118.       _Words() : _M_pword(0), _M_iword(0) { }
  119.     };

  120.     // Only for failed iword/pword calls.
  121.     _Words                  _M_word_zero;   

  122.     // Guaranteed storage.
  123.     static const int         _S_local_word_size = 8;
  124.     _Words                  _M_local_word[_S_local_word_size];  

  125.     // Allocated storage.
  126.     int                     _M_word_size;
  127.     _Words*                 _M_word;

  128.     _Words&
  129.     _M_grow_words(int __index);

  130.     // Members for locale and locale caching.
  131.     locale                 _M_ios_locale;

  132.     void
  133.     _M_init();

  134.   public:

  135.     // 27.4.2.1.6  Class ios_base::Init
  136.     // Used to initialize standard streams. In theory, g++ could use
  137.     // -finit-priority to order this stuff correctly without going
  138.     // through these machinations.
  139.     class Init
  140.     {
  141.       friend class ios_base;
  142.     public:
  143.       Init();
  144.       ~Init();
  145.       
  146.       static void
  147.       _S_ios_create(bool __sync);
  148.       
  149.       static void
  150.       _S_ios_destroy();

  151.     private:
  152.       static int         _S_ios_base_init;
  153.       static bool        _S_synced_with_stdio;
  154.     };

  155.     // Fmtflags state:
  156.     inline fmtflags
  157.     flags() const { return _M_flags; }

  158.     inline fmtflags
  159.     flags(fmtflags __fmtfl)
  160.     {
  161.       fmtflags __old = _M_flags;
  162.       _M_flags = __fmtfl;
  163.       return __old;
  164.     }

  165.     inline fmtflags
  166.     setf(fmtflags __fmtfl)
  167.     {
  168.       fmtflags __old = _M_flags;
  169.       _M_flags |= __fmtfl;
  170.       return __old;
  171.     }

  172.     inline fmtflags
  173.     setf(fmtflags __fmtfl, fmtflags __mask)
  174.     {
  175.       fmtflags __old = _M_flags;
  176.       _M_flags &= ~__mask;
  177.       _M_flags |= (__fmtfl & __mask);
  178.       return __old;
  179.     }

  180.     inline void
  181.     unsetf(fmtflags __mask) { _M_flags &= ~__mask; }

  182.     inline streamsize
  183.     precision() const { return _M_precision; }

  184.     inline streamsize
  185.     precision(streamsize __prec)
  186.     {
  187.       streamsize __old = _M_precision;
  188.       _M_precision = __prec;
  189.       return __old;
  190.     }

  191.     inline streamsize
  192.     width() const { return _M_width; }

  193.     inline streamsize
  194.     width(streamsize __wide)
  195.     {
  196.       streamsize __old = _M_width;
  197.       _M_width = __wide;
  198.       return __old;
  199.     }

  200.     static bool
  201.     sync_with_stdio(bool __sync = true);

  202.     // Locales:
  203.     locale
  204.     imbue(const locale& __loc);

  205.     inline locale
  206.     getloc() const { return _M_ios_locale; }

  207.     // Storage:
  208.     static int
  209.     xalloc() throw();

  210.     inline long&
  211.     iword(int __ix)
  212.     {
  213.       _Words& __word = (__ix < _M_word_size)
  214.                         ? _M_word[__ix] : _M_grow_words(__ix);
  215.       return __word._M_iword;
  216.     }

  217.     inline void*&
  218.     pword(int __ix)
  219.     {
  220.       _Words& __word = (__ix < _M_word_size)
  221.                         ? _M_word[__ix] : _M_grow_words(__ix);
  222.       return __word._M_pword;
  223.     }

  224.     // Destructor
  225.     ~ios_base();

  226. [color=red]  protected:
  227.     ios_base();
  228. [/color]
  229. #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
  230.   //50.  Copy constructor and assignment operator of ios_base
  231. [color=red]  private:
  232.     ios_base(const ios_base&);
  233. [/color]
  234.     ios_base&
  235.     operator=(const ios_base&);
  236. #endif
  237.   };
复制代码

这里面并没有看到static成员函数呀。cin, cout之类的是怎么实例化的呢?
发表于 2003-11-24 10:54:45 | 显示全部楼层

  1.   private:
  2.     ios_base(const ios_base& );
复制代码

是用来禁止拷贝构造函数。如果不显式地禁止,当调用拷贝构造函数时,编译器会自动提供一个拷贝构造函数。




  1.   protected:
  2.     ios_base();
复制代码

表明这个类必须继承,只能通过派生类来创建这个类的实例。
 楼主| 发表于 2003-11-24 11:42:25 | 显示全部楼层
那是不是他的派生类的实例创建也只有通过static成员或者friend来创建?
发表于 2003-11-24 11:59:41 | 显示全部楼层
不是,保护成员对派生类是可见的。派生类的成员函数可以直接调用基类的构造函数。能否直接创建派生类的实例,是由派生类的构造函数决定,与基类的构造函数无关。
 楼主| 发表于 2003-11-24 12:09:33 | 显示全部楼层
谢谢,刚刚试验了一下,知道是怎么回事了。
是我前面犯了个错误。
当基类的构造函数是protected时,派生类的构造函数要在初始化列表中调用基类的构造函数。我前面是在函数里面调用了,所以没有通过编译。 
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表