From: http://www.cnblogs.com/chio/archive/2007/07/18/822389.html First review C + + type conversions:
C + + type conversions are divided into: implicit type conversions and explicit type conversions
1th part. Implicit-type conversions
Also known as "standard conversions," includes the following:
1 Arithmetic conversion (arithmetic conversion): In the arithmetic expression of a mixed type, the widest data type becomes the target conversion type.
int ival = 3;
Double dval = 3.14159;
Ival + dval; Ival is promoted to double type
2 A type expression is assigned to an object of another type: The target type is the type of the assigned object, int * pi = 0; 0 is converted to int * type
Ival = Dval; Double->int
Exception: No standard conversions exist for null pointer assignment to other specified type pointers, compilation error
3 An expression is passed as an argument to a function call when the formal parameter and argument types are inconsistent: the target transformation type is the type of the formal parameter extern double sqrt (double);
cout << "The square root of 2 is" << sqrt (2) << Endl;
2 promoted to double type: 2.0
4 Returns an expression from a function, the expression type is inconsistent with the return type: The target conversion type is the return type of the function double difference (int ival1, int ival2)
{
return ival1-ival2;
The return value is promoted to the double type
}
2nd part. Explicit type conversions
called Coercion type conversion (CAST)
C style: (Type-id)
C + + style: static_cast, dynamic_cast, reinterpret_cast, and const_cast.
On the issue of coercion type conversion, many books have been discussed, the most detailed writing is the father of C + + design and evolution of C + +. The best solution is not to use the C-style coercion type conversion, but rather to use the standard C + + type conversion character: Static_cast, dynamic_cast. There are four types of conversion characters in standard C + +:static_cast、dynamic_cast、reinterpret_castAndconst_cast。 Here's a description of them.
static_cast
Usage:static_cast< Type-id > (expression)
Description: This operator converts expression to a type-id type, but does not have run-time type checking to guarantee the security of the conversion.
Source: Why you need to static_cast a cast. Case 1:void Pointer-> other type pointers Condition 2: Change the usual standard conversion Scenario 3: Avoid ambiguity about possible multiple conversions
It is mainly used for the conversion of pointers or references between base classes and subclasses in the class hierarchy. It is safe to perform an upward conversion (the pointer or reference of the handle class is converted to a base class representation), and it is unsafe to perform a downward conversion (converting the base class pointer or reference to a subclass pointer or reference) because there is no dynamic type checking. For conversions between basic data types, such as converting an int to char and converting an int to an enum. The security of this transformation also needs to be ensured by developers. Converts a void pointer to a pointer to a target type (unsafe!!) Converts an expression of any type to a void type. Note: static_cast cannot convert the const, Volitale, or __unaligned attributes of expression.
dynamic_cast
Usage:dynamic_cast< Type-id > (expression)
Description: This operator converts expression to an object of type Type-id. Type-id must be a pointer to a class, a reference to a class, or void *; if Type-id is a class pointer type, then expression must also be a pointer, and if Type-id is a reference, then expression must also be a reference.
Source: Why you need to dynamic_cast a cast. Simply put, when the virtual function is not available
Typical case: Wicrosoft Company provides us with a class library, which provides a class employee. Distribute to users with header file Eemployee.h and class library. lib Obviously we can't get the source code for the implementation of the class Emplyee.h Class Employee { Public virtual int salary (); };
Class Manager:public Employee { Public int salary (); };
Class Programmer:public Employee { Public int salary (); }; Our company in the development of time to establish the following categories: Class MyCompany { Public void Payroll (Employee *pe); // };
void MyCompany::p ayroll (Employee *pe) { Do something } But in the late stages of development, we want to add a bonus () member function to the class level provided by the w$ company. Assuming we know the source code, it's simple to add a virtual function: Emplyee.h Class Employee { Public |