开发者

C++ : Initializing base class constant static variable with different value in derived class?

开发者 https://www.devze.com 2022-12-28 03:29 出处:网络
I have a base class A with a constant static variable a. 开发者_运维问答I need that instances of class B have a different value for the static variable a. How could this be achieved, preferably with s

I have a base class A with a constant static variable a. 开发者_运维问答I need that instances of class B have a different value for the static variable a. How could this be achieved, preferably with static initialization ?

class A {
public:
    static const int a;
};
const int A::a = 1;

class B : public A {
    // ???
    // How to set *a* to a value specific to instances of class B ?
};


You can't. There is one instance of the static variable that is shared by all derived classes.


Static members are unique in the application. There is a single A::a constant in your system. What you can do is create a B::a static constant in B that will hide the A::a static (if you don't use the fully qualified name:

class A {
public:
   static const int a = 10;
};
static const int A::a;
class B : public A {
public:
   static const int a = 20;
   static void test();
};
static const int B::a;
void B::test() {
   std::cout << a << std::endl;    // 20: B::a hides A::a
   std::cout << A::a << std::endl; // 10: fully qualified
}


You can do this with Curiously recurring template pattern (you'll have to lose the const though).

template <typename T>
class A {
public:
    static int a;
};

template <typename T>
int A<T>::a = 0;

class B : public A<B> {
    struct helper { // change the value for A<B>::a
        helper() { A<B>::a = 42; }
    };
    static helper h;
};
B::helper B::h;


May be we can try this way as below :: The benefit of the below is that you don't have to write the code multiple times, but the actual generated code might be big.

#include <iostream>

using namespace std;
template <int t>
class Fighters {
protected :
    static const double Fattack;
    double Fhealth;
    static const double Fdamage;
    static int count;
public :
    Fighters(double Fh) : Fhealth(Fh) { }
    void FighterAttacked(double damage) {
        Fhealth -= damage;
    }
    double getHealth()
    {
        return Fhealth;
    }

    static int getCount() 
    {
        //cout << count << endl;
        return count;
    }
};

const double Fighters<1>::Fdamage = 200.0f;
const double Fighters<1>::Fattack = 0.6f;
int Fighters<1>::count = 0;

class Humans : public Fighters<1> {
public :
    Humans(double Fh = 250) : Fighters<1>(Fh) { count++; }
};

const double Fighters<2>::Fdamage = 40.0f;
const double Fighters<2>::Fattack = 0.4f;
int Fighters<2>::count = 0;

class Skeletons : public Fighters<2> {
public :
    Skeletons(double Fh = 50) : Fighters<2>(Fh) { count++; }
};

int main()
{

    Humans h[100];
    Skeletons s[300];

    cout << Humans::getCount() << endl;
    cout << Skeletons::getCount() << endl;

    return 0;
}

This is part of my other code example .. don't mind many other data but concept can be seen.

0

精彩评论

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

关注公众号