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_castAnd
const_cast。 Here's a description of them.
static_cast
Usage:
static_cast< Type-id > (expression)
This operator converts expression to a type-id type, but does not have run-time type checking to guarantee the security of the conversion. It mainly has the following several usages: conversion of pointers or references between base classes and subclasses in class hierarchies 。 It is safe to perform an upward conversion (a pointer or a reference to a base class representation) for a downward conversion (which converts a base class pointer or reference to a subclass representation) and is unsafe 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. & nbsp; converts an empty pointer to a null pointer of the target type. · converts any type of expression 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)
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 be a reference.
Dynamic_cast is mainly used for upstream and downlink transitions between class hierarchies, and for cross conversion between classes.
The effect of dynamic_cast and static_cast is the same in the upstream conversion between class levels, and the dynamic_cast has the function of type checking, which is more secure than static_cast in the downlink conversion. Class b{
Public
int m_inum;
virtual void foo ();
};
Class D:public b{
Public
Char *m_szname[100];
};
void func (B *pb) {
D *PD1 = Static_cast<d *> (pb);
D *pd2 = Dynamic_cast<d *> (pb);
}
In the preceding code snippet, if PB points to a D-type object, PD1 and PD2 are the same, and any operation of type D for both pointers is safe, but if the PB points to a type B object, then PD1 will be a To a pointer to the object, it is unsafe to perform a type D operation (such as accessing M_szname), and PD2 will be a null pointer. In addition to note: B to have virtual functions, or will compile errors; static_cast does not have this limitation. This is because Run-time type checking requires Run-time type information, and this information is stored in the virtual function table of the class (the concept of the virtual function table, which is detailed in the <inside C + + object model>), only the class that defines the virtual function has a virtual letter Table, there are no virtual function tables for classes that do not define virtual functions.
Other than that
dynamic_castCross-conversion (cross cast) is also supported. Shown in the following code. Class a{
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service