C ++:在派生类中初始化基类常量静态变量具有不同的值? [英] C++ : Initializing base class constant static variable with different value in derived class?

查看:136
本文介绍了C ++:在派生类中初始化基类常量静态变量具有不同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类A和一个常量静态变量a。我需要类B的实例具有不同的值为静态变量a。这怎么可能实现,最好用静态初始化?

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.

这篇关于C ++:在派生类中初始化基类常量静态变量具有不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆