The purpose of this article is to briefly introduce the C type conversion system and some discussions on the use and expansion. Transfer from: http://www.polyrandom.com/
C introduces new explicit type conversion methods such as const_cast, reinterpret_cast, not only most C programmers don't feel very habit, even some experienced C programmers will make mistakes on some details. It is true that since we can easily write:
INT i = (int) p; //p is a pointer
Such explicit conversion, why is it necessary to use?
INT i = reinterpret_cast
What is this complex form?
The purpose of this article is to briefly introduce the C type conversion system and some discussions on the use and expansion.
1. Why do you need type conversion? Type conversion is used to convert a type of value into another type. Programming languages similar to C are strong types, so each value has its corresponding type. When you need to convert a value to another type, you need to use one of the following: implicit conversion, explicit conversion, and cannot be converted. Suppose we use old-fashioned explicit conversions:
Char c = 'a'; int * p = null; int a = c; // Implicit conversion A = (int) p; // Explicit conversion double d = (double) p; // cannot be converted
Usually, implicit conversions means that your transformation is reasonable or safe; explicit conversion means that the compiler can find a conversion method, but it does not guarantee whether this conversion is safe, so you need to point out for programmers; And cannot be converted means that the compiler cannot find a direct path for type conversion.
2. Why do I need an explicit conversion of C style? Explicit conversion of C style provides us with more accurate semantics and further expansion of it. In C language, we can use a simple (int *) to complete the following conversion:
Const char * s = 0; int * p = (int *) s;
This sentence, not only converts the type, but also puts the conste. Usually, if we see a free explicit conversion, we cannot immediately know the author's intentions, which also buried a film for future mistakes. Realistic conversion of C style to increase security by distinguishing between various conversion: CONST_CAST to cancel the modification of const, volatile, and do related types of conversions via Static_cast, through ReinterPret_Cast to do low-level conversions, .... There is an example to indicate the "precise" degree of these conversions:
Class interface {Int member;};
Class base {};
Class Derface: Public Interface, Public Base {};
INT main () {base * b = 0; derived * d1 = reinterpret_cast
In this code, both CAST is legal, but the meaning is different. The former means "assigns D1 directly to D1, and explains it as a DeriveD type", the latter means "switching according to related type information, if possible, some offsets for the B pointer". " In this example, D1 and D2 are unequal! Maybe because many books say: If you want to transform each other between the pointers, you should use ReinterPret_cast, so many programmers use ReinterPret_cast to do all pointer type conversions, although they will not get errors, but they are indeed not small. Hidden dangers. 3. Some examples
(1) ITF_cast In COM, if I have a interface pointer pointing to IHTMLDocument, and I want to get an IPERSISTFILE pointer, we can use the following code:
IPERSISTFILE * PPERSISTFILE; if (failed (phtmldocument-> queryinterface (iid_ipersistfile, (lpvoid *) & ppersistfile))).
This code is very simple but not enough, so we can implement such a function:
Template
Then we can write the above statement
PPERSISTFILE = ITF_CAST (PHTMLDocument);
This is very intuitive. Careful readers may find that __UUIDOF is not defined by the standard C , but an extension of the VC. In fact, here you can use Traits very simple to achieve the same function.
(2) STREAM_CAST Sometimes, we often encounter some custom types to convert problems, such as std :: string, and double, even std :: string, and RECT translation. If the two types involved in the conversion define the input and output operation (accurately, the source type supports output operation, the purpose type support input operation), then we can use the following way:
Template
In this way, you can use the following statement to convert:
String S ("0.5"); Double D = stream_cast
(3) Safe_cast Sometimes we hope that our explicit conversion is safe. The security definition here is that this value is not damaged in two types of Zhonghe. That is, (T2) (T1) V1 == V1. Then we can define such an explicit conversion:
Template
The above is some of my individual's understanding and opinion of Explicit type conversion of C style, and I hope that people who are interested will be discussed.