首页 新闻 会员 周边

在模版的使用中,如何初始化一个静态嵌套类类型的对象?

0
[待解决问题]

template <class T>
class Init
{
public:
  Init* Clone()
  {
    return new Init(*this);
  }
  static Init* InitInstance()
  {
    cout << "BaseCreater::InitInstance()" << endl;
    return _pSingletonBase;
  }

  class Recycle
  {
  public:
    Recycle()
    {
      _pSingletonBase = new Singleton<T>();
      cout << "分配内存!Singleton::Recycle()" << endl;
    }
    ~Recycle()
    {
      if(NULL != _pSingletonBase)
      {
        delete _pSingletonBase;
        _pSingletonBase= NULL;
      }

      cout << "回收单例的内存!Singleton::~Recycle()" <<endl;
    }
  };

protected:

  friend class Recycle;

  static Recycle _recycle;

private:
  static Init* _pSingletonBase;
};


template<class T>
Init<T>* Init<T>::_pSingletonBase = NULL;

template<class T>
class Init<T>::Recycle Init<T>::_recycle;  // 提问, 这里的静态 Init<T>::_recycle对象并没有进行 类型的初始化,怎么将他进行初始化呢?

无根的泪痕的主页 无根的泪痕 | 菜鸟二级 | 园豆:204
提问于:2012-06-18 07:15
< >
分享
所有回答(1)
0

实践中发现要是static Recycle _recycle;是属于protected的成员

那么需要显示的调用一次 _recycle才能算是初始化了;

另外,

  如果使用

template<>

Init<int>::Recycle Init<int>::_recycle;进行显示的初始化

也得在外面的成员中进行一次 public的调用才能算是将 _recycle出事化;

 

 

是否有人 能用更加好的方式进行将

template<class T>

class Init{

...

protected:

  static Recycle _recycle

};

 

的_recycle进行初始化呢

无根的泪痕 | 园豆:204 (菜鸟二级) | 2012-06-18 19:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册