In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is C++ forced type conversion". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought slowly and deeply. Let's study and learn what C++ forced type conversions have.
1. C cast and C++ cast
C language cast is mainly used for conversion between basic data types, and the syntax is as follows:
(type-id) expression// conversion format 1type-id (expression) / / conversion format 2
In addition to the mandatory type conversion in C language, C++ also adds four new cast types: static_cast, dynamic_cast, const_cast, and reinterpret_cast, which are mainly used for casting between inheritance relationship classes. The syntax is as follows:
Static_cast (expression) dynamic_cast (expression) const_cast (expression) reinterpret_cast (expression)
Note: new_type is the target data type, and expression is the original data type variable or expression.
In "Effective C++", the c language forced type conversion is called the old style transformation, and the C++ forced type conversion is called the new type transformation.
2. Static_cast 、 dynamic_cast 、 const_cast 、 reinterpret_caststatic_cast
Static_cast is equivalent to the traditional C language cast, the operator converts expression to new_type type, used to force implicit conversion, such as non-const object to const object, compile-time check, for non-polymorphic conversion, can convert pointers and other, but there is no run-time type checking to ensure the security of the conversion. It has the following main uses:
① is used to convert pointers or references between base classes (parent classes) and derived classes (subclasses) in a class hierarchy.
It is safe to perform an uplink conversion (converting a pointer or reference of a derived class to a base class representation)
When performing downlink conversions (converting base class pointers or references to derived class representations), it is not safe because there is no dynamic type checking.
② is used for conversion between basic data types, such as converting int to char and int to enum. The security of this transformation also needs to be guaranteed by the developer.
③ converts null pointers to null pointers of the target type.
④ converts any type of expression to a void type.
Note: static_cast cannot convert the const, volatile, or _ _ unaligned attributes of expression.
Examples of basic type data conversion are as follows:
Char a ='a charter int b = static_cast (a); / / correct, convert char data into int data * c = new double;void * d = static_cast (c); / / correct, convert double pointer to void pointer int e = 10 int f = static_cast (e); / / correct, convert int data into const int data const int g = 20 int * h = static_cast (& g) / / compilation error. Static_cast cannot convert the const attribute of g
Class uplink and downlink transformations
If (Derived * dp = static_cast (bp)) {/ / Downlink conversion is unsafe / / use Derived object pointed to by dp} else {/ / use Base object pointed to by bp} if (Base*bp = static_cast (dp)) {/ / Uplink conversion is secure / / use Derived object pointed to by bp} else {/ / use Base object pointed to by dp} dynamic_castdynamic_cast (e) dynamic_cast (e) dynamic_cast (e)
Type must be a class type, in the first form, type must be a valid pointer, in the second form, type must be a left value, and in the third form, type must be a right value. In all the above forms, the type of e must meet any of the following three conditions: the type of e is a public derivative of the target type type, the type of e is the common base class of the target type, or the type of e is the type of the target type. If the conversion target of a dynamic_cast statement is a pointer type and fails, the result is 0. If the conversion target is a reference type and fails, the dynamic_cast operator throws a std::bad_cast exception (which is defined in the typeinfo standard library header file). E can also be a null pointer, resulting in a null pointer of the desired type.
Dynamic_cast is mainly used for uplink and downlink transformations between class levels, and can also be used for cross-conversion between classes (cross cast).
When uplink conversion between class levels, dynamic_cast and static_cast have the same effect.
When performing downstream conversions, dynamic_cast has the function of type checking, which is more secure than static_cast. Dynamic_cast is the only action that cannot be performed by the old syntax, and the only transformational action that can cost a lot of money to run.
(1) pointer type
For example, Base is a base class that contains at least one virtual function, and Derived is a common derivative of Base. If there is a pointer to Base, we can convert it to a pointer to Derived at run time, as follows:
If (Derived * dp = dynamic_cast (bp)) {/ / use Derived object pointed to by dp} else {/ / use Base object pointed to by bp}
It is worth noting that in the above code, dp is defined in the if statement, which has the advantage that both type conversion and conditional checking can be done in one operation.
(2) reference type
Because there is no so-called null reference, the dynamic_cast conversion of the reference type is different from the pointer type. When the reference conversion fails, a std::bad_cast exception is thrown, which is defined in the header file typeinfo.
Void f (const Base & b) {try {const Derived & d = dynamic_cast (b); / / use the Derived object referenced by b} catch (std::bad_cast) {/ / handle the case of type conversion failure} const_cast
Const_cast, used to modify the const or volatile property of a type.
This operator is used to modify the const (the only C++-style transformation operator with this capability) or the volatile property of the type. Except for const or volatile decorations, new_type and expression are of the same type.
The ① constant pointer is converted to a non-constant pointer and still points to the original object
② constant references are converted to non-constant references and still point to the original object
③ const_cast is generally used to modify the bottom pointer. Such as the form of const char * p.
An example conversion is as follows:
Const int g = 20int * h = const_cast (& g); / / remove the const constant const attribute const int g = 20int & h = const_cast (g); / / remove the const reference const attribute const char * g = "hello"; char * h = const_cast (g); / / remove the const pointer const attribute reinterpret_cast
New_type must be a pointer, reference, arithmetic type, function pointer, or member pointer. It can convert a pointer into an integer or an integer into a pointer (first convert a pointer to an integer, then convert the integer to a pointer of the original type, and get the original pointer value).
Reinterpret_cast is intended to perform a low-level transformation, and the actual action (and result) may depend on the editor, which means it is not portable.
To give an example of incorrect use of reintepret_cast, after converting an integer type to a function pointer, vc++ will report an unhandled exception: 0xC0000005: Access violation "error at the 0xxxxxxxxx in". "
# include using namespace std;int output (int p) {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.