About the explicit keyword

Source: Internet
Author: User

Today, we can see that the company's code contains a large number of explicit it keywords, but the examples in the old version of msdn are not perfect. I really don't understand it. I finally found the answer from the previous article on the Internet: originally, explicit was designed to prevent the implicit use of the copy constructor. the example and online article found in the new msdn version are attached as follows:

// Copy from msdn

This keyword is a declaration specifier that can only be applied to in-class constructor declarations. An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object.

The following program will fail to compile because ofExplicitKeyword. To resolve the error, removeExplicitKeywords and adjust the code ing.

// spec1_explicit.cpp// compile with: /EHsc#include 
 
  class C {public:   int i;   explicit C(const C&)   // an explicit copy constructor   {      printf("/nin the copy constructor");   }   explicit C(int i )   // an explicit constructor   {      printf("/nin the constructor");   }   C()   {      i = 0;   }};class C2{public:   int i;   explicit C2(int i )   // an explicit constructor   {   }};C f(C c){   // C2558   c.i = 2;   return c;   // first call to copy constructor}void f2(C2){}void g(int i){    f2(i);   // C2558   // try the following line instead   // f2(C2(i));}int main(){   C c, d;   d = f(c);   // c is copied}
 

NoteExplicitOn a constructor with multiple arguments has no effect, since such constructors cannot take part in implicit conversions. however, for the purpose of implicit conversion, explicit will have an effect if a constructor has multiple arguments and all but one of the arguments has a default value.

 

// Copy from Internet article

PointerI don't know how bad my c ++ is. I encountered a problem when I saw the explicit. See the following program:
Class {
Public:
A (int I): m_ I (I ){}
Int m_ I;
};
Int main (){
A A = 0;
A = 10; // What is the operation here?
}

This operation generates a temporary object.
I suspect that it is the default value assignment operator "A & operator = (int I) {}", so I overloaded the operator and the result is indeed running in the overloaded operator function, so how is a temporary object generated?

Is the default value assignment operator like this?
A & operator = (int I ){
A A (I );
Return;
}

This reminds me of similar function operations:
Void FN (){
//...
}
FN (10) can be directly written here; a temporary object is also generated.

Is that true? Forget to confuse.

AlexeyomuxDude, which compiler are you using? In my impression, it seems that the standard C ++ does not provide operator =. Wait for me to give it a try. GongminminOf course there will be the default operator =
According to the C ++ standard, the compiler generates five default member functions:
Default constructor
Copy constructor
Destructor
Operator =
Operator &   Qianma GaNClass
{
Public:
A (int I): m_ I (I ){}
Int m_ I;
};

Let's talk about it separately:
1. a A = 0;
First, compiler considers that writing in this way does not conform to the rules, because a = A is normal.
However, she does not give up. By searching, she finds that a can be constructed based on an int, and this a (int I) has not been modified by explicit.
Then a = 0; such a sentence is converted:
A tmp (0 );
A A = TMP;
It should be noted that a A = TMP is the called copy ctor, although class A does not, but usually does not write the copy ctor,
Compiler generates a ctor for the memberwise assignment operation. The underlying implementation is usually performed by memcpy.

2. A = 10;
In this case, compiler cannot perform operations directly, just like the ctor.
And so on, equivalent to code:
A tmp (10 );
A = TMP;
It should be noted that a = TMP is the assignment operation called. Like the ctor, we do not write it ourselves, and the compiler does the same.
Memberwise assignment operation.

3. FN ()
Similarly, FN (10) is not correct, but "according to the Convention", haha, there will be:
A tmp (10 );
FN (TMP );

In addition, you can solve the following problems:
Copy ctor can only be written as T: T (const T &);
However, assignment can be written in a variable way, that is, Tue. Take T as an example,
Yes
T & operator = (int n );
You can also
T & operator = (const char *);
Of course, you need to confirm that such definition is meaningful to T.

Then, the above a = TMP, that is, the default, standard, and automatically generated T & operator = (const T &) of the call &).
The overhead is generated by a temporary a tmp and then memcpy.
However, if you write T & operator = (int n), A = 10 means a. m_ I = 10.
Of course, in terms of overhead, it depends on whether your T & operator = (int n) is inline.

For explicit, when explicit a (int I): m_ I (I) {} is modified, compiler is told not to perform so many conversions in private.
In addition, we do not want to automatically generate things such as a tmp (0), because automatic conversion is wrong in some cases.

Finally, there is another topic about this kind of problem, that is, Class A can have operator int (),
FN (int n ){}
A A (3 );
FN ()
It plays a magic role. Let's take a look at the book for yourself :)

Finally, I wish you a smooth journey to C ++. Good luck ~

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.