0

I don't understand how the derived class gets its base class

class cl
{
  protected:
  int fe;
};

class lc : cl
{
public:
  fe = 10; //How can lc access cl? I know its a base class, but how is it stored?
};

How does lc get cl's members?

  • The derived class contains a subobject of the base class' type. Observe that `sizeof(lc) >= sizeof(cl)`. Members inherited from the base class are stored in that subobject. – Igor Tandetnik Jun 28 '20 at 21:07
  • what is a sub-object? –  Jun 28 '20 at 21:08
  • It works as if `lc` had a hidden member variable of type `cl`. – HolyBlackCat Jun 28 '20 at 21:10
  • Why do you need to know "how does lc get cl's members"? What problem are you trying to solve that you think you need to know this, for? – Sam Varshavchik Jun 28 '20 at 21:16
  • @SamVarshavchik Is curiosity not a valid reason anymore? :P – HolyBlackCat Jun 28 '20 at 21:20
  • The `fe` member is stored within the Base Class region. The Derived Class encompasses the Base Class. The derived class can't access the Base class protected members since the inheritance is `private` by default. – Thomas Matthews Jun 28 '20 at 21:21
  • 1
    @ThomasMatthews Again, whether the inheritance is `private` or not doesn't affect what members the derived class can access. It only matters when *something else* tries to access those members *in an instance of the derived class*. – HolyBlackCat Jun 28 '20 at 21:22
  • @SamVarshavchik i dont understand how derived classes work if i dont know why –  Jun 28 '20 at 21:22
  • 3
    I can guarantee you it is completely possible to learn how derived class work without knowing anything about the underlying physical storage. Somehow, this is fully explained [in every C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) without the need to go into these kinds of details. – Sam Varshavchik Jun 28 '20 at 21:31
  • If I'm not mistaken, the answer to your question is an implementation detail that can vary from compiler to compiler. A general answer is the compiler knows the layout of the base and derived classes, so therefore knows how to access the base members from the derived object. – Eljay Jun 28 '20 at 22:45

1 Answers1

1

Unless you are doing something special, declare your derived class as having public inheritance. Otherwise, C++ defaults to private inheritance and blocks methods in the child class from having access to the parent class's members. That is:

class lc : public cl
{
};

Also, this line won't compile as you have it inside the declaration of the child class.

  fe = 10; //How can lc access cl? I know its a base class, but how is it stored?

Do this instead. initialize fe in your constructor:

class lc : public cl
{
public:
  lc() : fe(10)
  {
  }
};

Or just do something like this after doing the public inheritance fix above:

lc item;
item.fe = 10;

As to your "how does it work?" question. Implementation details are not usually not a part of the C++ standard, but the most likely implementation is this. Under the hood, the compiler silently decorates your child class declaration with all the same members of the parent class. As if you had copied the original class declaration, renamed it, and manually added all the new members yourself directly below the original parent members. That's what enables you to say item.fe = 10 above, even though fe wasn't declared as a member of lc.

selbie
  • 100,020
  • 15
  • 103
  • 173