Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does C++ use delegated constructors to achieve common actions of all constructors?

Shulou Source: shulou.com Published: 2022-06-01 19:11:52 10月01日 Update

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!

Tags: Function action delegate Category content example learning practical ordinary deeper similar boring unexpected interest reason negative practical practical difference suggestion Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi Shulou Information Redmi OPPO Reno