C++ :在派生类中用不同的值初始化基类常量静态变量?

发布于 2024-08-29 20:23:21 字数 266 浏览 4 评论 0原文

我有一个带有常量静态变量 a 的基类 A。我需要类 B 的实例对静态变量 a 具有不同的值。如何实现这一点,最好使用静态初始化?

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 ?
};

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 ?
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-09-05 20:23:21

你不能。所有派生类共享静态变量的一个实例。

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

白昼 2024-09-05 20:23:21

静态成员在应用程序中是唯一的。您的系统中有一个 A::a 常量。您可以做的是在 B 中创建一个 B::a 静态常量,它将隐藏 A::a 静态(如果您不这样做) t 使用完全限定名称:

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
}

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
}
执妄 2024-09-05 20:23:21

您可以使用 奇怪的重复模板模式(您必须丢失 const 虽然)。

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;

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;
若水般的淡然安静女子 2024-09-05 20:23:21

也许我们可以尝试以下方式::
下面的好处是你不必多次编写代码,但实际生成的代码可能会很大。

#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;
}

这是我的其他代码示例的一部分..不介意许多其他数据,但可以看到概念。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文