Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C++ use inherited constructors

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report