In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how C++ forced type conversion, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
I. C forced conversion
The cast in C language is mainly used for the cast of common data types and pointers, there is no type checking, and the conversion is not safe.
The syntax is:
(type-id) expression// conversion format 1type-id (expression) / / conversion format 2 (basically no longer used) 2. C++ forced conversion
In addition to the mandatory type conversion in C language, C++ has also added four kinds of cast: static_cast, dynamic_cast, const_cast, reinterpret_cast
It is mainly used in the forced conversion between inheritance relationship classes, and the syntax is:
/ / static conversion static_cast (expression) / / dynamic conversion dynamic_cast (expression) / / constant conversion const_cast (expression) / / reinterpret conversion reinterpret_cast (expression)
New type is the new type after conversion, and expression is the old type.
1. Static_cast static conversion (compile-time check)
Usage: static_cast (variable or expression)
Static_cast static conversion is equivalent to the mandatory conversion in C language, but it can not realize the mandatory conversion of ordinary pointer data (except null pointers). It is generally used for the conversion between parent and subclass pointers and references.
Used for the conversion of pointers or references between base classes (parent classes) and derived classes (subclasses) in the class hierarchy. Regardless of whether polymorphism occurs or not, the compiler will not report an error when the father and son switch to each other.
(1) it is safe to perform an uplink conversion (converting a pointer or reference of a derived class to a base class representation)
(2) when performing downlink conversion (converting a base class pointer or reference to a derived class representation), it is not safe because there is no dynamic type checking, but the compiler will not report an error.
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 a null pointer to a null pointer of the target type.
Converts any pointer type to a null pointer type.
Note: static_cast cannot convert the const, volatile, or _ _ unaligned attributes of expression
When classes are involved, static_cast can only convert to each other among types that are related to each other, and does not necessarily contain virtual functions.
In the C++ language, static_cast is used to cast data types, forcing one data type to another. For example, convert integer data to floating-point data.
[example 1] the type conversion method adopted by C language:
# include using namespace std;int main () {int a = 10; int b = 3; double result = (double) a / (double) b; 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.