Static_cast and dynamic_cast

Source: Internet
Author: User
Static_cast

Usage: static_cast <type-ID> (expression)
This operator converts expression to the Type-ID type, but does not check the runtime type to ensure the conversion security. It has the following usage:
① It is used to convert pointers or references between classes and subclasses in the class hierarchy.
It is safe to perform upstream conversion (converting the pointer or reference of a subclass to a base class;
When performing a downstream conversion (converting a base class pointer or reference to a subclass), it is not safe because there is no dynamic type check.
② It is used for conversion between basic data types. For example, convert int to Char and convert int to enum. The security of such conversions must also be ensured by developers.
③ Convert a null pointer to a null pointer of the target type.
④ Convert any type of expression to void type.
Note: static_cast cannot convert the const, volitale, or _ unaligned attribute of expression.
Differences between static_cast and reinterpret_cast in C ++
In chapter 5 of C ++ primer, the compiler implicitly executes any type conversion, which can be displayed by static_cast. reinterpret_cast generally provides a lower-level re-interpretation for the bit mode of the operand.
1. static_cast in C ++ performs non-polymorphism conversion instead of the normal conversion operation in C. Therefore, it is used as an implicit type conversion. For example:
Int I;
Float F = 166.7f;
I = static_cast <int> (f );
In this case, the result is that the I value is 166.
2. reinterpret_cast in C ++ converts data from one type to another. The so-called "usually provides lower-level re-interpretation for the bit mode of the operand" means re-interpretation of the data in the form of binary existence. For example:
Int I;
Char * P = "this is a example .";
I = reinterpret_cast <int> (P );
In this case, the values of I and P are exactly the same. Reinterpret_cast is used to interpret the value of P as an integer in binary (bit mode) mode and assign it to I. An obvious phenomenon is that there is no digital loss before and after conversion.

Dynamic_cast

Usage: dynamic_cast <type-ID> (expression)
This operator converts expression to type-ID objects. Type-ID must be a class pointer, class reference, or void *;
If type-ID is a class pointer type, expression must also be a pointer. If type-ID is a reference, expression must also be a reference.
Dynamic_cast is mainly used for upstream and downstream conversions between classes, and can also be used for cross conversions between classes.
When performing upstream conversion between classes, dynamic_cast and static_cast have the same effect;
Dynamic_cast provides the type check function for downstream conversions, which is safer than static_cast.
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 );
}
Above Code If Pb points to a D-type object, pd1 and Pd2 are the same, and it is safe to execute any operations of the D-type on the two pointers;
However, if PB points to a B-type object, pd1 will be a pointer to this object, it is not safe to perform operations of the D type (for example, to access m_szname ),
Pd2 is a null pointer.
Note: B must have virtual functions; otherwise, compilation errors may occur. static_cast does not have this restriction.
This is because the runtime type check requires runtime type information, which is stored in the virtual function table of the class (
For more information about the concept of virtual function tables, see <inside C ++ object model>). Only classes that define virtual functions have virtual function tables,
Classes that do not define virtual functions do not have virtual function tables.
In addition, dynamic_cast also supports cross cast ). The following code is used.
Class {
Public:
Int m_inum;
Virtual void F (){}
};
Class B: Public {
};
Class D: Public {
};
Void Foo (){
B * pb = new B;
Pb-> m_inum = 100;
D * pd1 = static_cast <D *> (PB); // compile Error
D * Pd2 = dynamic_cast <D *> (PB); // Pd2 is null
Delete Pb;
}
In function Foo, using static_cast for conversion is not allowed, and errors will occur during compilation. Using dynamic_cast for conversion is allowed, and the result is a null pointer.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.