"Go" explicit keyword in C + +

Source: Internet
Author: User

in C + + programs Few people go to use the explicit keyword, admittedly, in peacetime practice rarely can be used. Again, C + + is powerful, and often a problem can be solved with several C + + features. But a little attention will find that the current MFC library or the C + + standard library in the related class declaration explicit appear in a very high frequency. Understanding the features of the explicit keyword and its use is helpful for us to read and use libraries, and can also be used when writing your own code. Since the C + + language provides this feature, I think this feature will be very useful in some cases. By Default, an implicit conversion is also defined by a constructor that passes only one argument. As an example:write a class cexample, whose declaration is written in the header file Example.h, as follows:
1 //Example.h2 #pragmaOnce3 classCexample4 { 5  Public: 6Cexample (void); 7  Public: 8~cexample (void); 9 ­Ten  Public:  One intM_ifirst; A intM_isecond; -  Public:  -Cexample (intIfirst,intIsecond =4);  the};

The implementation of the class is written in Example.cpp, as follows:

 "StdAfx.h" "Example.h"   Cexample::cexample (void): M_ifirst (0) {} cexample::~cexample (void  ) {} cexample::cexample (intint

The main functions are as follows:

1 //TestExplicitKey.cpp2...//Other header Files3#include"Example.h" 4 int_tmain (intARGC, _tchar*argv[])5 { 6Cexample Objone;//call a constructor with no arguments7Cexample Objtwo ( A, A);//call a constructor with two parameters8Cexample Objthree ( A);//As above , you can pass a parameter because the second parameter of the constructor has a default value9Cexample Objfour = A;//An implicit conversion is performed, equivalent to Cexample temp, objfour (temp); Notice that this place calls theTen //The default copy constructor provided to us by the compiler One return 0;  A}
   if the keyword explicitis added to the constructor declaration, the following explicit cexample (int ifirst, int isecond = 4); then cexample objfour = 12; This statement will not compile. The error is as follows:
 error C2440:  " initializing   ' : cannot convert from   " int   " to "  cexample             constructor  for  "  cexample   " is  declared  '  

  For some types, this is a very desirable situation. In most cases, however, implicit conversions can easily lead to errors (not syntax errors, and the compiler will not error). Implicit conversions are always happening silently without our awareness, and unless the intent is done, implicit conversions are often something we don't want to happen. Implicit conversions can be suppressed by declaring the constructor as explicit (explicit). In other words, the explicit constructor must be explicitly called.

  Cite the example of Bjarne Stroustrup:  

1 classstring{2ExplicitString (intN); 3String (Const Char*p); 4 }; 5String S1 ='a';//error: Cannot do an implicit char->string conversion6String S2 (Ten);//can: Call explicit String (int n);7String s3 = String (Ten);//You can: Call explicit String (int n), and then call the default copy constructor8String S4 ="Brian";//You can: implicitly convert the call string (const char *p), and then call the default copy constructor9String S5 ("Fawlty");//yes: normal call to String (const char *p);Ten voidf (String); One ­ A String g () - {  -FTen);//error: Cannot do an implicit int->string conversion theF"Arthur");//can: implicit conversion, equivalent to F (String ("Arthur")); -return Ten;//Ibid . -}
   something in the actual code is not like this deliberately created example. implicit conversions occur, and implicit conversions often result in procedural logic errors, which are difficult to detect once they occur, unless they are deliberately exploited. In principle, you should add the explicit keyword to all constructors before you use implicit conversion to remove explicit, which can greatly reduce the occurrence of errors.      Transfer from http://blog.csdn.net/chollima/article/details/3486230

"Go" explicit keyword in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.