In C ++ programming languages, there are two types of conversions: implicit conversion and explicit conversion. In this article, we will introduce the various situations of C ++ implicit conversions in detail, hoping to help you get some help.
C ++ implicit conversion occurs in four situations
* In a mixed-type arithmetic expression
- Int ival = 3;
- Double dvals = 3.1415
- Ival + dval; // ival is upgraded to double: 3.0
* Assign values using a type of expression
- Int * pi = NULL; // NULL (0) is converted to a NULL pointer of the int * type.
* Pass an expression to a function call
- Extern double sqrt (double );
- Sqrt (2); // 2 is upgraded to double type: 2.0
* Returns an expression from a function.
- Double difference (int ival1, int ival2)
- {
- Return ival1-ival2; // the return value is elevated to the double type.
- }
C ++ built-in types (char, int, short, double etc.) include implicit conversions by default between images.
C ++ user-defined class objects can contain C ++ implicit conversions.
- Void dosomething (A aObject );
- Class {
- Public:
- A (int x = 0 );
- }
- Dosomething (20); // OK implicit conversion
The above is an introduction to the implicit conversion of C ++.