How does C++ use inherited constructors
This article mainly explains "how C++ uses inherited constructors". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how C++ uses inherited constructors".
C.52: use the inherited constructor function to import the constructor into a derived class Reason that no longer requires further explicit initialization (reason)
If derived classes need those constructors, the work of reimplementing them is tedious and error-prone.
Example (sample)
Std::vector has a large number of constructors that are difficult to use, so if I need my own vector, I won't reimplement them.
Class Rec {
/ /... Data and lots of nice constructors...
}
Class Oper: public Rec {
Using Rec::Rec
/ /... No data members...
/ /... Lots of nice utility functions...
}; Example, bad (negative example) struct Rec2: public Rec {
Int x
Using Rec::Rec
}
Rec2 r {"foo", 7}
Int val = r.x; / / uninitialized
This is an example of the need for further initialization. If the derived class does not add data members but just adds some functionality, you can use the using Rec::Rec method to import the constructor of the base class. For the above example, you can also consider initializing the data member x using an in-class initializer.
Ensure that all members of the derived class are initialized.
At this point, I believe you have a deeper understanding of "how C++ uses the inherited constructor". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!