类A可能被很多类继承;  
现在要找到一种机制,便每一个A的直接子类(如B或C或D),都有各自的一个int型的静态成员a;  
但是这些直接子类之间不分享这个成员,也就是说很个直接子类都有各自的a.  
就是说C::a=1这条语句对B::a的值没有任何影响就像MFC中每一个CmdTarget类的子类都会有一个自己的静态命令路由表;
但是MFC是通过宏来实现的。要求是继承的类不必有任何一句关于这个成员的声明,只要继承自A,它就马上自动获得一个专属的(不于其它兄弟类共享的)静态成员int a.以前有人曾经有模板实现过。100分不够可再加。

解决方案 »

  1.   

    template <typename T>
    class CParent {
    ...
    }
    然后
    typedef CParent<int> CChild1;
    typedef CParent<int> CChild2;
    不可以吗?
      

  2.   

    经过C++ QQ组的讨论最后结果如下:
    using std::cout;template<typename T> 
    class A
    {
    public:
        static int i;
    };template<typename T>
    int A<T>::i = 0;class B:public A<B>
    {};class C:public A<C>
    {};int main()
    {
     C::i=1;
     B::i=2;
     cout<<C::i;

      

  3.   

    嗯,不错
    很久没用模版了,我下面的程序就不如你的好了,呵呵#include <iostream>using namespace std;template <typename T>
    class CParent {
    public:
        static int i;
    };template<typename T> int CParent<T>::i = 0;typedef CParent<int> CChild1;
    typedef CParent<float> CChild2;int main()
    {
        CChild1::i = 1;
        CChild2::i = 2;
        cout << CChild1::i << CChild2::i << endl;
    }