Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use static_cast conversion in C++

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use static_cast conversion in C++, the article is very detailed, has a certain reference value, interested friends must read it!

Static_cast is a cast operator. Cast, also known as explicit conversion, C++ cast operators static_cast, dynamic_cast, const_cast, reinterpert_cast four. This section describes the static_cast operator.

Conversion of static_cast

Any type conversion implicitly performed by the compiler can be done by static_cast, such as conversion between int and float, double and char, enum and int, and so on.

Doublea=1.999

Intb=static_cast (a); / / equivalent to astatb

When compilers implicitly perform type conversions, most compilers give a warning:

E:\ vs2010projects\ static_cast\ static_cast\ static_cast.cpp (11): warningC4244: "initialization": convert from "double" to "int". Data may be lost.

Using static_cast, you can explicitly tell the compiler that this loss of precision conversion is done knowingly, and it can also let other programmers reading the program know the purpose of your conversion rather than negligence.

Convert the type with high precision to the type with low precision, and static_cast uses bit truncation to deal with it.

Using static_cast, you can retrieve the value stored in the void* pointer.

Doublea=1.999

Void*vptr=&a

Double*dptr=static_cast (vptr)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report