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 to use the delegate constructor in Category 11

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

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.

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