gpt4 book ai didi

c++ - 静态变量继承 C++

转载 作者:行者123 更新时间:2023-11-30 05:03:52 25 4
gpt4 key购买 nike

<分区>

我想要派生类中的静态变量,所以我想在基类中做一些东西。基类将是虚拟的。可能吗?

class Base {
public:
static const int x;
void f() {
return x;
}
virtual void g() = 0;
};

class Derived1 : virtual Base {
public:
void g() {}
};
const int Derived1::x = 1;

class Derived2 : virtual Base {
public:
void g() {}
};
const int Derived2::x = 2;

...

Derived1 a;
Derived2 b;
a.f() == 1; // should be true
b.f() == 2; // should be true

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com