In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-11-03 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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.