In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use the explicit keyword in C++. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1 implicit conversion
The default type of the C++ constructor is implicit, which can be defined with or without the identifier.
/ / A class declaration that does not use the explicit keyword, that is, the default is to implicitly declare class CString {private: char * mSecretiSize; public: CxString (int size) {m_iSize = size; m_pStr = malloc (m_iSize + 1); memset (m_pStr, 0, m_iSize + 1) } CxString (const char * p) {int size = strlen (p); m_pStr = malloc (size + 1); strcpy (m_pStr, p); m_iSize = strlen (m_pStr);} ~ CxString () {if (m_pStr) {delete masks; m_pStr = nullptr;}
The following methods are correct when instantiating an object of a class in the above code.
As follows:
CString str1 ("12121"); / CxString (const char * p) CString str2= "abc"; / / CxString (const char * p) CString str4 (4); / CxString (int size) CString str5=6;//CxString (int size)
All of the above methods call the corresponding constructor to initialize the object by default. Taking CString str5=6 as an example, the following operations are performed during the instantiation of the object:
CString string5 (6); / / or CString temp (6) as shown below; CString string5 = temp;2 display conversion
After using the explicit keyword on the constructor, the display type conversion is required when the object is instantiated through the constructor. Otherwise, an error will be reported.
Still take the above code as an example:
Class CString {private: char * mroompStrings; int melixiSize; public: explicit CString (int size) {m_iSize = size; m_pStr = (char *) malloc (m_iSize + 1); memset (m_pStr, 0, m_iSize + 1);} explicit CString (const char * p) {int size = strlen (p) M_pStr = (char *) malloc (size + 1); strcpy (m_pStr, p); m_iSize = strlen (m_pStr);}}
As above: an error will be reported when you instantiate the class again in the same way, as shown below:
The running result shows that the explicit keyword can prevent the implicit automatic conversion of the constructor.
3 disassemble again
The explicit keyword is valid only for the form that the constructor has one parameter, and the function of explicit will be invalidated if the constructor has more than one parameter, but C++ also provides an exception, that is, if all other parameters of the constructor have default values and the number of parameters is greater than 1 explicit.
As shown in the following code:
Class CPoint {private: float mummerfX; float mummerfY; public: explicit CPoint (float x Magneto float yawn 9.8): m_fX (x), m_fY (y) {}}; int main () {CPoint point1 (2mae 3); CPoint point2=4.5; return 0;}
As shown in the code, an error will be reported at compile time, with the following message:
If you want to solve the above problem, you can use the following method:
It is handled in the way of display type conversion, such as:
CPoint point1 (2); CPoint point2 (4. 5)
Remove the explicit keyword to allow the constructor to implicitly convert data types, such as:
CPoint (float x float yearly 9.8): m_fX (x), m_fY (y) {} Thank you for reading! This is the end of the article on "how to use the explicit keyword in C++". I hope the above content can be of some help to you, so that you can learn more knowledge. 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.
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.