Are static fields inherited?

前端 未结 7 1184
孤街浪徒
孤街浪徒 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:27

    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

    0 讨论(0)
提交回复
热议问题