首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

用auto_ptr实现的Singleton模式中为什么要把auto_ptr<Singleton>设成友元类?解决办法

2012-03-05 
用auto_ptr实现的Singleton模式中为什么要把auto_ptrSingleton设成友元类?下面的代码是用auto_ptr实现的

用auto_ptr实现的Singleton模式中为什么要把auto_ptr<Singleton>设成友元类?
下面的代码是用auto_ptr实现的Singleton模式:
class   Singleton
{
public:
              static   Singleton   *   Instance()
              {
                            if(   0==   _instance.get())
                            {
                                          _instance.reset(   new   Singleton);
                            }
                            return   _instance.get();
              }
protected:
              Singleton(void)
              {
                            cout   < < "Create   Singleton " < <endl;
              }
              virtual   ~Singleton(void)
              {
                            cout   < <   "Destroy   Singleton " < <endl;
              }
              friend   class   auto_ptr <Singleton> ;
              static   auto_ptr <Singleton>   _instance;
};

我不明白为什么要加上friend   class   auto_ptr <Singleton> ;这句呢?



[解决办法]
因为构造和析构 是 protected 的


[解决办法]
template <class T>
auto_ptr
{
auto_ptr
{
T = new T();
}
~auto_ptr()
{
T::~T()
}
}
大体意思。。

热点排行