C++ 静态字段是否继承?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/998247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are static fields inherited?
提问by
When static members are inherited, are they static for the entire hierarchy, or just that class, i.e.:
当继承静态成员时,它们是整个层次结构的静态成员,还是该类的静态成员,即:
class SomeClass
{
public:
SomeClass(){total++;}
static int total;
};
class SomeDerivedClass: public SomeClass
{
public:
SomeDerivedClass(){total++;}
};
int main()
{
SomeClass A;
SomeClass B;
SomeDerivedClass C;
return 0;
}
would total be 3 in all three instances, or would it be 2 for SomeClass
and 1 for SomeDerivedClass
?
在所有三个实例中,total 是 3,还是 2 forSomeClass
和 1 for SomeDerivedClass
?
采纳答案by Alex Martelli
3 in all cases, since the static int total
inherited by SomeDerivedClass
is exactly the one in SomeClass
, not a distinct variable.
3 在所有情况下,因为static int total
继承的SomeDerivedClass
正是 中的一个SomeClass
,而不是一个不同的变量。
Edit: actually 4in all cases, as @ejames spotted and pointed out in his answer, which see.
编辑:实际上在所有情况下都是4,正如@ejames 在他的回答中发现并指出的那样,见。
Edit: the code in the second question is missing the int
in both cases, but adding it makes it OK, i.e.:
编辑:第二个问题中的代码在int
两种情况下都缺少,但添加它就可以了,即:
class A
{
public:
static int MaxHP;
};
int A::MaxHP = 23;
class Cat: A
{
public:
static const int MaxHP = 100;
};
works fine and with different values for A::MaxHP and Cat::MaxHP -- in this case the subclass is "not inheriting" the static from the base class, since, so to speak, it's "hiding" it with its own homonymous one.
工作正常,并且 A::MaxHP 和 Cat::MaxHP 的值不同——在这种情况下,子类“不继承”基类的静态,因为可以这么说,它用自己的同名“隐藏”了它一。
回答by e.James
The answer is actually fourin all cases, since the construction of SomeDerivedClass
will cause the total to be incremented twice.
在所有情况下,答案实际上都是4,因为 的构造SomeDerivedClass
将导致总数增加两倍。
Here is a complete program (which I used to verify my answer):
这是一个完整的程序(我用来验证我的答案):
#include <iostream>
#include <string>
using namespace std;
class SomeClass
{
public:
SomeClass() {total++;}
static int total;
void Print(string n) { cout << n << ".total = " << total << endl; }
};
int SomeClass::total = 0;
class SomeDerivedClass: public SomeClass
{
public:
SomeDerivedClass() {total++;}
};
int main(int argc, char ** argv)
{
SomeClass A;
SomeClass B;
SomeDerivedClass C;
A.Print("A");
B.Print("B");
C.Print("C");
return 0;
}
And the results:
结果:
A.total = 4
B.total = 4
C.total = 4
回答by VenuGopal
It is 4 because when the derived object is created, the derived class constructor calls the base class constructor.
So the value of the static variable is incremented twice.
是4,因为在创建派生对象时,派生类构造函数调用了基类构造函数。
所以静态变量的值增加了两次。
回答by rocky4android
#include<iostream>
using namespace std;
class A
{
public:
A(){total++; cout << "A() total = "<< total << endl;}
static int total;
};
int A::total = 0;
class B: public A
{
public:
B(){total++; cout << "B() total = " << total << endl;}
};
int main()
{
A a1;
A a2;
B b1;
return 0;
}
It would be:
这将是:
A() total = 1
A() total = 2
A() total = 3
B() total = 4
回答by Darko Maksimovic
SomeClass() constructor is being called automatically when SomeDerivedClass() is called, this is a C++ rule. That's why the total is incremented once per each SomeClass object, and then twice for SomeDerivedClass object. 2x1+2=4
调用 SomeDerivedClass() 时会自动调用 SomeClass() 构造函数,这是 C++ 规则。这就是为什么每个 SomeClass 对象的总数增加一次,然后 SomeDerivedClass 对象增加两次。2x1+2=4
回答by Niki Yoshiuchi
Yes, the derived class would contain the same static variable, i.e. - they would all contain 3 for total (assuming that total was initialized to 0 somewhere).
是的,派生类将包含相同的静态变量,即 - 它们都将包含总计 3(假设在某处将总计初始化为 0)。
回答by adzm
3 in all three instances.
在所有三种情况下均为 3。
And for your other question, it looks like you really just need a const variable instead of static. It may be more self-explanatory to provider a virtual function that returns the variable you need which is overridden in derived classes.
对于你的另一个问题,看起来你真的只需要一个 const 变量而不是静态变量。提供一个虚函数可能更容易解释,该函数返回您需要的变量,该变量在派生类中被覆盖。
Unless this code is called in a critical path where performance is necessary, always opt for the more intuitive code.
除非在需要性能的关键路径中调用此代码,否则请始终选择更直观的代码。