0

I've just always been curious. Under the hood, what happens to the inherited classes?

Gardevoir
  • 13
  • 3
  • 4
    Why do you expect the behaviour to change compared to inheriting a non-`const` variable, or inheriting from a concrete class? – Quentin Jul 02 '18 at 14:04
  • The base class instance is contained within the derived class instance, so the derived instance doesn't really "get" anything in the sense you seem to imagine (there's neither copying nor indirection involved). – molbdnilo Jul 02 '18 at 14:13
  • Derived classes *are* the type of their base classes, so they have access to all of their (non-private) parent base class members. The compiler knows that `Derived::static_base_var` is the same variable as `Base::static_base_var`. – David R Tribble Jul 02 '18 at 14:49
  • 2
    To be clear, are you asking about a `static const` member or a non-static `const` member? – François Andrieux Jul 02 '18 at 15:09
  • How the const variable is used will also affect what the compiler will generate. For example, if you cast away the const and modify the value, the compiler will reserve memory for the variable. The compiler may also decide to inline the const value, and not generate a variable in memory at all. –  Jul 02 '18 at 22:05

0 Answers0