In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about how to construct explicit functions, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
By default, a constructor with only one parameter also defines an implicit conversion to convert the data of the corresponding data type of the constructor into such an object, as shown below: class String {String (const char* p); / / use the C-style string p as the initialization value / /... } String S1 = "hello"; / / OK implicit conversion, equivalent to String S1 = String ("hello"); but sometimes this implicit conversion may not be needed, as follows: class String {String (int n); / / it is meant to pre-allocate n bytes to the string String (const char* p); / / use the C-style string p as the initialization value / /... } the following two ways of writing are relatively normal: String S2 (10); / / OK's 10-byte empty string String S3 = String (10); / / OK's 10-byte empty string: String S4 = 10; / / compiled, also 10-byte empty string String S5 ='a' / / the empty strings S4 and S5 that allocate int ('a') bytes implicitly convert an int type and a char type into an empty string allocated several bytes, which is easily misleading. To avoid this error, we can declare the conversion shown, using the explicit keyword: class String {explicit String (int n); / / it is meant to pre-allocate n bytes to the string String (const char* p); / / use the C-style string p as the initialization value / /... } adding explicit suppresses the implicit conversion of String (int n), and the following two words are still correct: String S2 (10); / / OK assigned 10 bytes of empty string String S3 = String (10); / / OK allocated 10 bytes of empty string the following two ways of writing are not allowed: String S4 = 10; / / compilation failed, implicit conversion of String S5 ='a'is not allowed / / compilation fails, implicit conversion is not allowed, so, in some cases, explicit can effectively prevent errors or misunderstandings caused by implicit conversion of constructors.
Explicit works only on constructors to suppress implicit conversions. Such as:
Class A {
A (int a)
}
Int Function (An a)
When Function (2) is called, 2 is implicitly converted to type A. This situation is often not what the programmer wants, so to avoid it, you can write:
Class A {
Explicit A (int a)
}
Int Function (An a)
In this way, when Function (2) is called, the compiler gives an error message (unless Function has an overloaded form that takes int as an argument), which avoids errors without the programmer's knowledge.
Summary: explicit works only on constructors to suppress implicit conversions.
After reading the above, do you have any further understanding of how explicit constructs functions? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.