Are static fields inherited?

64,506

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

#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

Solution 5

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

Share:
64,506

Related videos on Youtube

BartoszKP
Author by

BartoszKP

I like TDD and clean architectures with clean code inside. My first language was C++, learned when I was young. After I started working as a software engineer there was a lot of switching between C++ and .NET/C#, and thus I really like both languages. I also like Python very much, and have some experience. I had also a very brief period using Java, and a lot of small encounters with languages like Prolog, Fortran, and Lisp. Later I was working as a SCRUM master and then as an architect and supervisor of multiple SCRUM teams. In 2018 I've been working with hearing instruments at Sonova WSC as a .NET Software Engineer. Since 2019 I'm working at Google.

Updated on June 29, 2020

Comments

  • BartoszKP
    BartoszKP about 4 years

    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?

  • Mathias
    Mathias about 15 years
    Good explanation, but the numerical answer is actually 4, not 3. See my answer (stackoverflow.com/questions/998247/…)
  • Alex Martelli
    Alex Martelli about 15 years
    +1, Excellent point, I'm editing the answer to point to yours, thanks!
  • Damon
    Damon over 12 years
    +1, though one should more correctly say "+4 to whatever the static member is initialized to". The static member is neither local scope nor namespace scope, so there must be a definition somewhere that assigns a value (not necessarily zero). Otherwise the code does not fulfill the one-definition-rule and won't compile.
  • LRDPRDX
    LRDPRDX almost 7 years
    But If one wants static int total be distinct for each derived class the only way to achieve it to add static int total to each class? Or is it possible to use only base class definition (?), because having variable total should be the property of every class. On the other hand it should be static.