Talk about the explicit keyword

xiaoxiao2021-03-06  115

Today, I saw a lot of Explicit keywords in the company's code, but the old version of the MSDN is not perfect, I really don't understand, I finally found the answer from an online article: The original explicit is to prevent implicit use Constructor. The following is attached to the example from the new MSDN and the article on the Internet:

// Copy from MSDN

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

The Following Program Will Fail To Compile Because of the expendure. To resolve the error, remove the expendure,.

// SPEC1_EXPLICIT.CPP

// compile with: / eHSC

#include

Class C

{

PUBLIC:

INT I;

Explicit C (Const C &) // AN Explicit Copy Constructionor

{

Printf ("/ nin the copy constructor);

}

Explicit C (INT I) // AN Explicit Constructionor

{

Printf ("/ nin the constructor");

}

C ()

{

i = 0;

}

}

Class C2

{

PUBLIC:

INT I;

Explicit C2 (INT I) // An Explicit Constructionor

{

}

}

C f (c c)

{// c2558

C.I = 2;

Return C; // First Call to Copy Construction

}

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

}

Note

explicit on a constructor with multiple arguments has no effect, since such constructors can not 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

Pointer

Don't read the book, I don't know how to have a lot of difference T_T, seeing an issue when I see ExPlicit. Please see a program below:

Class a {

PUBLIC:

A (INT I): M_i (i) {}

INT M_I;

}

Int main () {

A a = 0;

A = 10; // What is the operation here?

}

This operation produces a temporary object.

I suspect that the default assignment operator "A & Operator = (INT i) {}, so that the operator is overloaded, the result is indeed running to the overloaded operator function, then how the temporary object is generated? ? Is the default assignment operator do this?

A & Operator = (INT I) {

A a (i);

Return A;

}

This reminds me of a similar function to operate:

Void Fn (a a) {

// ...

}

Here you can write FN (10) directly; it also generates a temporary object.

Is it true? I forget.

Alexeyomux

Which compiler is you used? In my impression, it seems that standard C will not give Operator =. Wait for me to try it.

gongminmin

Of course there will be default operator =

According to the C standard, the compiler generates five default member functions:

Default constructor

Copy constructor

Destructor

Operator =

Operator &

Kilomeliver

Class A

{

PUBLIC:

A (INT I): M_i (i) {}

INT M_I;

}

Tell you separately:

1. a a = 0;

First, Compiler believes that this is not in line with the rules, because A = a is normal behavior.

But she does not give up, through search, found A can be constructed according to an int structure, and this A (INT i) is not modified with ExPlicit.

So A a = 0; this sentence is then transformed into:

A TMP (0);

A a = TMP;

It should be noted that a a = TMP is called Copy CTOR, although there is no Class A, but usually does not write a Copy Ctor,

COMPILER will generate a CTOR of a MEMBERWISE ASSIGNMENT operating nature, and the underlying implementation is usually made in Memcpy.

2. a = 10;

First, like this to the CTOR, Compiler cannot operate directly.

Category push, equivalent to code:

A TMP (10);

a = TMP;

It should be noted that A = TMP is called Assignment operation, like CTOR, we don't write, the compiler is also

MEMBERWISE ASSIGNMENT operation.

3. Fn (a a)

Similarly, Fn (10) is not right, but "according to the practice", huh, it will be:

A TMP (10);

Fn (TMP);

In addition, for you:

Copy CTOR will only be T :: T (Const T &);

The ASSIGNMENT's way of writing can be changeable, let alone. T as T,

You may have

T & Operator = (int N);

Can also

T & Operator = (const char *);

Of course, you have to confirm that this is the meaning of T.

Then, the above A = TMP, that is, the default, standard, automatically generated T & Operator = (Const T &).

The overhead will have a temporary A TMP generation, then Memcpy.

But if you have written T & Operator = (int N), then A = 10 means A.M_I = 10.

Of course, in terms of overhead, regard your T & Operator = (int N) is inline.

For ExPlicit, when modified Explicit A (INT I): M_i (i) {}, then telling Compiler not to do so many conversion actions in private.

And what is automatically generated such as A TMP (0) is what we don't want, because in some cases, this behavior is wrong.

Finally, there is a topic of this problem, that is, Class A can have Operator Int (), will be in Fn (int N) {}

A a (3);

Fn (a)

Get a magic role. About this, leave you yourself to see the book :)

Finally, I wish to learn C on the way. Good luck ~

转载请注明原文地址:https://www.9cbs.com/read-125304.html

New Post(0)