Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the delegate constructor in Category 11

Shulou Source: shulou.com Published: 2022-06-02 10:22:55 10月28日 Update

This article will explain in detail how to use the delegated constructor in Category 11. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The situation before September 11.

When there are more constructors, code duplication is almost inevitable, and to avoid this, it is often necessary to write another initialization function. For example, the following Rect class:

Struct Point {

Int x

Int y

}; struct Rect {

Rect () {

Init (0,0,0,0,0,0)

}

Rect (int l, int t, int r, int b) {

Init (l, t, r, b, lc, fc, 0,0)

}

Rect (int l, int t, int r, int b

Int lc, int fc) {

Init (l, t, r, b, lc, fc)

}

Rect (Point topleft, Point bottomright) {

Init (topleft.x, topleft.y

Bottomright.x, bottomright.y

0,0)

}

Init (int l, int t, int r, int b

Int lc, int fc) {

Left = l; top = t

Right = r; bottom = b

Line_color = lc

Fill_color = fc

/ / do something else...

}

Int left

Int top

Int right

Int bottom

Int line_color

Int fill_color

}

There is some other work to be done after the data member is initialized, which is necessary for every construction method, so another init function is prepared for each constructor to call.

This approach does avoid code duplication, but there are two problems:

There is no way to initialize the list of members without repeating it

You must write another initialization function.

The solution of Clear11

Cellular constructor 11 extends the function of constructor and adds the concept of delegated constructor so that one constructor can delegate other constructors to complete the work. After using the delegate constructor, the previous code looks like this:

Struct Point {

Int x

Int y

}

Struct Rect {

Rect ()

: Rect (0,0,0,0,0,0)

{

}

Rect (int l, int t, int r, int b)

: Rect (l, t, r, b, 0,0)

{

}

Rect (Point topleft, Point bottomright)

: Rect (topleft.x, topleft.y

Bottomright.x, bottomright.y

0,0)

{

}

Rect (int l, int t, int r, int b

Int lc, int fc)

: left (l), top (t), right (r), bottom (b)

Line_color (lc), fill_color (fc)

{

/ / do something else...

}

Int left

Int top

Int right

Int bottom

Int line_color

Int fill_color;}; about how to use the delegate constructor in Category 11 is shared here. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

Tags: Function code delegate work central committee content situation member article way more knowledge article yes two method function and again necessity data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Information vpn Huawei NVidia