In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to use the explicit keyword in C++. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
In C++, we can sometimes use the constructor as an automatic type conversion function. However, this automatic feature is not always desirable and can sometimes lead to unexpected type conversions, so C++ has added the keyword explicit to turn off this automatic feature. That is, the class constructor modified by the explicit keyword cannot be automatically implicitly converted, but can only be explicitly converted.
Note: only a constructor with one parameter, or a constructor with n arguments, but a default value provided by nMel 1 parameter, can be converted.
The following is a code to demonstrate the specific application (without explicit):
1 / * sample code 1 * / 2 class Demo 3 {4 public: 5 Demo (); / * constructor 1 * / 6 Demo (double a); / * sample code 2 * / 7 Demo (int a) / * sample code 3 * / 8 Demo (int a penning int baked 10 legendary double caches 1.6); / * sample code 4 * / 9 ~ Demo (); 10 void Func (void); 11 12 private:13 int value1;14 int value2;15}
The above four constructors:
Constructor 1 has no arguments and cannot be converted!
Constructor 2 has one parameter that can be cast, such as: Demo test; test = 12.2; such a call is equivalent to implicitly converting 12.2 to a Demo type.
Constructor 3 has two parameters and no default value, so type conversion cannot be used!
Constructor 4 has three parameters, two of which have default values, so it can be implicitly converted, such as: Demo test;test = 10;.
The following describes the use of the keyword explicit:
1 1 / * sample code 2 * / 22 class Demo 3 3 {4 4 public: 5 5 Demo (); / * constructor 1 * / 6 6 explicit Demo (double a); / * sample code 2 * / 7 7 Demo (int aline double b) / * sample code 3 * / 8 8 9 9 ~ Demo (); 10 10 void Func (void); 11 11 12 12 private:13 13 int value1;14 14 int value2;15 15}
In constructor 2 above, implicit conversion cannot be done due to the use of the explicit keyword. That is, Demo test;test = 12.2; is invalid! However, we can do display type conversion, such as:
Demo test
Test = Demo; or
Test = (Demo) 12.2
This is how the explicit keyword is used in C++, which is shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.