Inherited static fields?

When static members are inherited, whether they are static for the entire hierarchy or only for this class, that is:

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

will it be 3 in all three cases, or will it be 2 for SomeClass and 1 for SomeDerivedClass ?

+83
c ++ inheritance static
Jun 15 '09 at 20:38
source share
7 answers

3 in all cases, since the static int total inherited by SomeDerivedClass exactly matches SomeClass , not a separate variable.

Edit: actually 4 in all cases, as @ejames noticed and indicated in his answer that they see.

Edit: the code in the second question is absent in int in both cases, but adding it makes it OK, that is:

 class A { public: static int MaxHP; }; int A::MaxHP = 23; class Cat: A { public: static const int MaxHP = 100; }; 

It works fine and with different values ​​for A :: MaxHP and Cat :: MaxHP - in this case, the subclass does not "inherit" the static from the base class, because, so to speak, it "hides" it using its own eponymous.

+43
Jun 15 '09 at 20:41
source share

The answer is actually four in all cases, since building SomeDerivedClass will cause the amount to be doubled.

Here is the complete program (which I used to check 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 
+85
Jun 15 '09 at 20:47
source share

This is 4 because when the derived object is created, the constructor of the derived class calls the constructor of the base class.
Thus, the value of a static variable is doubled.

+7
Jan 12 2018-11-11T00:
source share
 #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; } 

This will:

 A() total = 1 A() total = 2 A() total = 3 B() total = 4 
+4
May 30 '12 at 9:30 a.m.
source share

3 in all three cases.

And for your other question, it looks like you just need a const variable instead of statics. For a provider, a virtual function that returns the required variable, which is overridden in derived classes, may be more understandable.

If this code is not invoked on a critical path where performance is required, always choose a more intuitive code.

0
Jun 15 '09 at 20:41
source share

Yes, the derived class will contain the same static variable, i.e. all of them will contain 3 for the total number (assuming that the amount was initialized somewhere to 0).

0
Jun 15 '09 at 20:42
source share

The SomeClass () constructor is called automatically when SomeDerivedClass () is called, this is a C ++ rule. Therefore, the total amount is increased once for each SomeClass object, and then twice for the SomeDerivedClass object. 2x1 + - = 4

0
Feb 14 '11 at 7:13
source share



All Articles