Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are static fields inherited?

People also ask

Do static fields get inherited?

Static members can be inherited to, in which case you can access them using the extending class name. This is true (known as hiding, as mentioned above), unless the member is final. Final static members cannot be hidden in subclasses if they were inherited by the subclass.

Are static Fields inherited in C++?

static member functions act the same as non-static member functions: They inherit into the derived class. If you redefine a static member, all the other overloaded functions in the base class are hidden.

Why static methods are not inherited?

Another thing to note is that you cannot override a static method, you can have your sub class declare a static method with the same signature, but its behavior may be different than what you'd expect. This is probably the reason why it is not considered inherited.


The answer is actually four in all cases, since the construction of SomeDerivedClass will cause the total to be incremented twice.

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

3 in all cases, since the static int total inherited by SomeDerivedClass is exactly the one in SomeClass, not a distinct variable.

Edit: actually 4 in all cases, as @ejames spotted and pointed out in his answer, which see.

Edit: the code in the second question is missing the int in both cases, but adding it makes it OK, i.e.:

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.


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.


#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

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