惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tenable Blog
WordPress大学
WordPress大学
小众软件
小众软件
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
大猫的无限游戏
大猫的无限游戏
T
The Exploit Database - CXSecurity.com
Attack and Defense Labs
Attack and Defense Labs
Simon Willison's Weblog
Simon Willison's Weblog
C
CXSECURITY Database RSS Feed - CXSecurity.com
量子位
有赞技术团队
有赞技术团队
C
Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
F
Fortinet All Blogs
S
Schneier on Security
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
Martin Fowler
Martin Fowler
Recent Announcements
Recent Announcements
Stack Overflow Blog
Stack Overflow Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Spread Privacy
Spread Privacy
F
Full Disclosure
Scott Helme
Scott Helme
GbyAI
GbyAI
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
Cloudbric
Cloudbric
云风的 BLOG
云风的 BLOG
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
Hacker News - Newest:
Hacker News - Newest: "LLM"
Security Latest
Security Latest
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
The Register - Security
The Register - Security
L
Lohrmann on Cybersecurity
PCI Perspectives
PCI Perspectives
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
D
Docker
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Secure Thoughts
C
Check Point Blog

博客园 - 至尊王者

使用 Android Lua Helper 在VSCODE中调试安卓Lua应用 lua语言特性及用途 Ollvm混淆与反混淆: goron框架字符串加密的实现原理 Ollvm混淆与反混淆: Goron框架编译与使用 安卓使用OLLVM混淆.so 雷电9中使用Fiddler抓包 ubuntu 22.04离线安装cuda 11.7.1、cudnn 8.9.3.28、nccl 2.18.3、tensorrt 8.6.1 C/C++程序隐藏符号 配置ETH 2.0 同步节点 Nethermind & Prysm rust在windows上编译openssl mac 远程桌面 the user account did not work的解决办法 MacOS静态编译ffmpeg 4.4.1、x264、x265等编解码库的脚本 Linux、Windows静态编译ffmpeg 4.4.1、x264、x265等编解码库的脚本 Visual Studio 2022 Key 在Docker中安装宝塔面板 idea/clion行注释位置(不在首列,注释就靠近代码块) linux手动编译llvm/clang linux与windows双系统保持时间同步 Puppeteer: 等待打开一个新页面
reinterpret_cast和其它cast的区别
至尊王者 · 2024-01-23 · via 博客园 - 至尊王者

C++中,有四种类型转换操作符:static_cast, dynamic_cast, const_castreinterpret_cast。它们的作用和区别是什么呢?

static_cast是最常用的一种类型转换,它可以在编译时进行基本类型之间的转换,也可以进行类层次结构中的向上或向下转换。例如:

int i = 10;
double d = static_cast<double>(i); // 基本类型转换
class A {};
class B : public A {};
A* a = new A();
B* b = static_cast<B*>(a); // 向下转换,不安全
a = static_cast<A*>(b); // 向上转换,安全

dynamic_cast主要用于类层次结构中的向下转换,它可以在运行时检查转换的合法性,如果转换失败,会返回空指针或抛出异常。例如:

A* a = new A();
B* b = dynamic_cast<B*>(a); // 向下转换,失败,返回空指针
a = new B();
b = dynamic_cast<B*>(a); // 向下转换,成功,返回非空指针

const_cast用于去除或添加const或volatile属性,它可以改变对象的底层const性。例如:

const int x = 10;
int* p = const_cast<int*>(&x); // 去除const属性
*p = 20; // 修改x的值,未定义行为
volatile int y = 10;
int* q = const_cast<int*>(&y); // 去除volatile属性
*q = 20; // 修改y的值

reinterpret_cast是最危险的一种类型转换,它可以将任意类型的指针或引用转换为任意类型的指针或引用,也可以将整数类型转换为指针类型或反之。它不会进行任何运行时检查或类型调整,只是简单地按位重新解释对象的内存表示。例如:

int i = 10;
char* c = reinterpret_cast<char*>(&i); // 将int*转换为char*
*c = 'A'; // 修改i的低字节为65
void* v = reinterpret_cast<void*>(i); // 将int转换为void*
i = reinterpret_cast<int>(v); // 将void*转换为int

reinterpret_cast和其它cast的区别主要体现在以下几个方面:

  • 转换范围:reinterpret_cast可以用于互不相关类型之间的转换,而其他cast只能用于相关类型之间的转换。
  • 安全性:reinterpret_cast不进行任何安全检查,不保证转换后的结果有任何意义,它只是按照程序员的意图进行强制类型转换。因此使用reinterpret_cast时要非常小心,它可能会导致未定义行为、数据丢失或程序崩溃,而其他cast会进行一定的安全检查,以减少安全隐患。
  • 移植性:reinterpret_cast的结果可能因编译器或硬件平台的不同而不同,因此具有较差的移植性,而其他cast的结果通常具有较好的移植性。

reinterpret_cast的使用场景主要有以下几种:

  • 底层操作,如将内存地址转换为指针或整数。
  • 调试工具,如将指针转换为整数以便在调试器中查看其值。
  • 特定的硬件平台,如在某些特定的硬件平台上,reinterpret_cast可以用于实现特定的功能。