Explicit & Volatile

xiaoxiao2021-03-06  40

I often see the explicit key for some time, because I am busy, I don't know what it means. During this time, I took the class and read the book, and finally understood its meaning.

Let me talk about the constructor as an implicit conversion function. In C , all classes have a configuration function that only one parameter, defines a set of implicit conversions, and convert the parameter type of the constructor to the class of the Class. For example, Integer (int) defines an implicit conversion from int to INTEGER type. For example, for the following code:

Extern Void Calc (Integer); INT I; Calc (i);

If the Integer class does not declares an Integer (int) constructor, the compiler will report at the CALC (i). Conversely, if an Integer class declares an Integer (int)-type constructor, the int value can be used in any place where the INTEGER type is used.

What should I do if we don't expect the compiler to automate this implicit conversion? There is a solution that stated it as an explicitIT:

Class integer {public: explicit integer (int); // ...};

This is when compiling to implicit type conversion, the compiler will report an error, indicating that implicit conversion cannot be performed. We can explicitly perform conversion operations in the form of forced conversion, such as:

Extern void function; int main () {func (99); // error, no implicit conversion FUNC (Integer (99)); // OK, Explicit conversion FUNC (static_cast (99)); // ok, forced conversion};

Volatile Keywords ...

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

New Post(0)