Are static fields inherited?

前端 未结 7 1183
孤街浪徒
孤街浪徒 2020-11-28 02:44

When static members are inherited, are they static for the entire hierarchy, or just that class, i.e.:

class SomeClass
{
public:
    SomeClass(){total++;}
           


        
相关标签:
7条回答
  • 2020-11-28 03:14

    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.

    0 讨论(0)
  • 2020-11-28 03:14

    3 in all three instances.

    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.

    Unless this code is called in a critical path where performance is necessary, always opt for the more intuitive code.

    0 讨论(0)
  • 2020-11-28 03:16

    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.

    0 讨论(0)
  • 2020-11-28 03:18

    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).

    0 讨论(0)
  • 2020-11-28 03:26
    #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
    
    0 讨论(0)
  • 2020-11-28 03:27

    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
    
    0 讨论(0)
提交回复
热议问题