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 delegated constructors to achieve common actions of all constructors?

2025-01-19 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 the delegated constructor to achieve the common action of all constructors", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how C++ uses delegated constructors to achieve the common actions of all constructors.

C.51: use delegated constructors to achieve common actions for all constructors

Reason (reason)

Avoid repetitive and unexpected differences.

Example, bad (negative example) class Date {/ / BAD: repetitive

Int d

Month m

Int y

Public:

Date (int dd, Month mm, year yy)

: d {dd}, m {mm}, y {yy}

{if (! valid (d, m, y)) throw Bad_date {};}

Date (int dd, Month mm)

D {dd}, m {mm} y {current_year ()}

{if (! valid (d, m, y)) throw Bad_date {};}

/ /...

}

Common actions are boring to write and occasionally become unusual.

Example (sample) class Date2 {

Int d

Month m

Int y

Public:

Date2 (int dd, Month mm, year yy)

: d {dd}, m {mm}, y {yy}

{if (! valid (d, m, y)) throw Bad_date {};}

Date2 (int dd, Month mm)

: Date2 {dd, mm, current_year ()} {}

/ /...

}; Enforcement (implementation recommendations)

(medium) look for constructors with similar function bodies.

At this point, I believe you have a deeper understanding of "how C++ uses delegated constructors to achieve the common actions of all constructors". 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