-2

I'm learning about class inheritance in C++ and read the following:

When constructing a derived class, its base class part is constructed first. The order of operations is:

1) The base class members are constructed

2) The base class constructor code is called

could someone explain the difference between 1 and 2? How could the base class members being constructed without calling the base class constructor? isn't that the constructor's job?

  • 2
    No. Read about member initializer lists: https://en.cppreference.com/w/cpp/language/constructor – jtbandes Jun 09 '20 at 23:18
  • 1
    Members (and base classes) are always constructed before the body of the constructor is entered. Anything you do to set a member inside the body of the constructor is assignment, not initialization. – user4581301 Jun 09 '20 at 23:21
  • 2
    You asked the exact same question 20 minutes ago. – Thomas Sablik Jun 09 '20 at 23:25
  • 1
    When constructing the base class, its members are *initialised* first (which, for members which are instances of another class, includes executing their constructor).The body of the base class is then executed to (for example) modify (e.g. reassign) values of the members post their initialisation. That occurs recursively - the process of constructing a derived class first executes constructors of bases, then initialises the members of the derived class, then the body of the derived class constructor. There's a slight difference if any bases are virtual, but the same general logic applies. – Peter Jun 09 '20 at 23:25
  • @ThomasSablik I deleted that since it was declared as "not specific" and wrote new improved one –  Jun 09 '20 at 23:26
  • You didn't improve it. You removed the image. The text is the same. I described it there. You are asking for the difference of two different things – Thomas Sablik Jun 09 '20 at 23:27
  • But what if I use initialisation list? it's job was to prevent re=assigning values, may you give some examples one for 1 and for 2? –  Jun 09 '20 at 23:28
  • @ThomasSablik it's not the same text, read it again –  Jun 09 '20 at 23:28
  • I can't. You deleted the question. – Thomas Sablik Jun 09 '20 at 23:29
  • the text there was exactly "Could someone explain what's the difference between 1 and 2" I don't see how this is the Same –  Jun 09 '20 at 23:32
  • 1
    Read the link in the first comment jtbandes for examples of member Initializer list usage. It's job is not to prevent reassignment. It's job is to initialize. – user4581301 Jun 09 '20 at 23:35
  • I read it , so constructing an object isn't equal to initialising it? where I can read more about this, this is what I was told! –  Jun 09 '20 at 23:42
  • You can find nearly everything about C++ on https://en.cppreference.com/w/cpp/language/constructor and it's human readable as opposed to the [standard](https://eel.is/c++draft/). – Thomas Sablik Jun 09 '20 at 23:46
  • See Peter's comment. Every base and member is initialized, even if the initialization does nothing, in the order the member is declared before entering the constructor's body. The member initializer list provides the opportunity to give special initialization instructions. Once everything is constructed and safe to use, you can use the body of the constructor to modify the members or take actions that you cannot safely perform before everything is fully constructed, call a function that takes `this` as a parameter for example. – user4581301 Jun 09 '20 at 23:57
  • The point is the object is as safe as possible to use BEFORE you get a chance to use it. – user4581301 Jun 09 '20 at 23:58
  • I'm torn on whether or not this question is solid enough to merit an answer, since you might have simply misread "base class constructor code" as "base class constructor". This is basically a typo-level issue. On the other hand, SO does recognize that some typo-level issues get answered in a way that could help future readers. I'm pretty sure I've seen this particular misread before (but didn't find a duplicate), so maybe an answer is warranted? – JaMiT Jun 10 '20 at 03:12

1 Answers1

1

Before trying to understand derived class construction, you should understand class construction. How is a class constructed? When constructing a class, its data members are constructed first. The order of execution within the constructor is:

  1. The class members are constructed.
  2. The constructor body is called.

How could the class members be constructed without calling the constructor? They aren't, as that is part of the constructor's job. The constructor is called, at which point the constructor constructs the class members, then the constructor executes its body (called its "code" in whatever you are reading).

MyClass::MyClass()   // <-- entry point for the constructor
                     // <-- construct members
{ /* do stuff */ }   // <-- the body/code

You can control member construction via an initialization list or you can fall back on default construction for the members.


Ready for inheritance? The only addition is that the base class is added to the beginning of the initialization list.

Derived::Derived()   // <-- entry point for the derived class constructor
                     // <-- construct base class (see below)
                     // <-- construct members
{ /* do stuff */ }   // <-- the body/code
Base::Base()         // <-- entry point for the base class constructor
                     // <-- construct members (a.k.a. step 1)
{ /* do stuff */ }   // <-- the body/code (a.k.a. step 2)

More complex, but basically the same thing as before.

JaMiT
  • 14,422
  • 4
  • 15
  • 31