开发者

static member definition with template base class

There is a base class:

template<class T_CLASS>
class TBase
{
  protected:
    static CSomeClass m_objSomeClass;

  public:
    inline void Set(CSomeClass f_objSomeClass) { m_objSomeClass = f_objSomeClass; }
};

And there are some sub classes which all shall have their own static member m_objSomeClass. I try to do this by templating the base class.

class CSub1 : public TBase<CSub1>
{
   //...
};

class CSub2 : public TBase<CSub2>
{
  //...
};

What does the definition for this look like? Is it even possible? I tried some... but none worked:

template<class T_CLASS>
CSomeClass TBase<T_CLASS>::m_objSomeClass;

//In fact the next one worked in Visual Studio; 
// but n开发者_JAVA百科ot in with the armcc where I need it.
CSomeClass TBase<CSub1>::m_objSomeClass;
CSomeClass TBase<CSub2>::m_objSomeClass;

Any suggestions? Thanks, Mirco


template<>
CSomeClass TBase<CSub1>::m_objSomeClass;
template<>
CSomeClass TBase<CSub2>::m_objSomeClass;

is the one of the correct ways, if you want to explicitly have static member defined for a solid class like CSub1, CSub2. Demo.

Edit: The conventional way is to define as:

template<class T_CLASS>
CSomeClass TBase<T_CLASS>::m_objSomeClass;

Both ways would serve the purpose.


And there are some sub classes which all shall have their own static member m_objSomeClass. I try to do this by templating the base class.

If all you want to achieve is separate static member, you don't need sub class, you can just instantiate from TBase and they will have separate static member, because each template class generated from a template has its own copies of any static variables or members, see the example below (the example compiles fine with VS2008 and gcc):

struct CSomeClass {
    CSomeClass(int i):m_i(i){}
    int m_i;
};

template<class T_CLASS>
class TBase
{
  protected:
    static CSomeClass m_objSomeClass;

  public:
    inline void Set(CSomeClass f_objSomeClass) { m_objSomeClass = f_objSomeClass; }
};

class CSub1
{

};

class CSub2
{
};

template<class T_CLASS>
CSomeClass TBase<T_CLASS>::m_objSomeClass = CSomeClass(0);

int main()
{
    TBase<CSub1> tb1;
    TBase<CSub2> tb2;

    //tb1 and tb2 have separate static member after instantiated from tempalte class TBase
    tb1.Set(CSomeClass(1)); //tb1::m_objSomeClass now is 1
    tb2.Set(CSomeClass(2)); //tb2::m_objSomeClass now is 2
}


Mirco, since you really need a subclass, I have revised my example so you have subclass and you have seperate static member function, see below(the example compiles fine with VS2008 and gcc):

struct CSomeClass {
    CSomeClass(int i):m_i(i){}
    int m_i;
};

template<class T_CLASS>
class TBase
{
  protected:
    static CSomeClass m_objSomeClass;

  public:
    inline void Set(CSomeClass f_objSomeClass) { m_objSomeClass = f_objSomeClass; }
};

class CSub1 : public TBase<CSub1>
{

};

class CSub2 : public TBase<CSub2>
{
};

template<class T_CLASS>
CSomeClass TBase<T_CLASS>::m_objSomeClass = CSomeClass(0);

int main()
{
    TBase<CSub1> tb1;
    TBase<CSub2> tb2;

    //tb1 and tb2 have separate static member after instantiated from tempalte class TBase
    tb1.Set(CSomeClass(1)); //tb1::m_objSomeClass now is 1
    tb2.Set(CSomeClass(2)); //tb2::m_objSomeClass now is 2
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜