In the constructor constructor constructor constructor, the constructor constructor default constructor requires a default constructor or initialize cat objects to avoid creating a default.
Why is the constructor for twoCats trying to call the default constructor for a cat? Of course, he does not need to create a cat instance, as when two Cats are initialized, will he be passed an already initialized cat instance, which will be passed an int height argument?
He needs to build default values ββfor cat objects
private: cat firstCat; cat secondCat;
in the twoCats class because you did not initialize them. In your constructor
cat::cat (int inputHeight) { height = inputHeight; ^^^^^^^^^^^^^^^^^^^^ }
this assignment to already created objects.
The rule is as follows: if you do not initialize the instances explicitly in the ctor initialization ctor , then
- By default,
ctor is called - Ultimately, you assign the already built default objects in the
ctor tag.
Thus, you will be punished for additional calls if you are not initialized in the initialization list.
C ++ Standard n3337 Β§ 12.6.2 / 10 Initialization of bases and members
In the non-delegated constructor, initialization continues in the following order:
- Firstly, and only for the constructor of the derived class itself (1.8), the virtual base classes are initialized in the order they appear on the depths of the first left-right directed acyclic graph base classes, where "from left to right" is the order in which base classes appear in the base -specifier of the derived class.
- Then direct base classes are initialized in the order of declaration as they appear in the list-specifier-base (regardless of the order of the MEM-initializers).
- Then non-static data elements are initialized in the order in which they were declared in the class definition (again, regardless of the order of the MEM initializers).
- Finally, the compound operator of the constructor body is executed .
[Note: the declaration order is authorized to ensure that the base and subobject member are destroyed in the reverse order of initialization. - final note]
Here is a demo of the code.