惯性聚合
高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文
在惯性聚合中打开
即将跳转到惯性聚合
3
在聚合应用中查看完整内容和互动
立即跳转
取消
推荐订阅源
S
Security Affairs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
CTFtime.org: upcoming CTF events
Jina AI
P
Palo Alto Networks Blog
GbyAI
大猫的无限游戏
A
Arctic Wolf
Hugging Face - Blog
小众软件
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Blog — PlanetScale
S
Schneier on Security
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
雷峰网
T
Tenable Blog
人人都是产品经理
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
AWS News Blog
Microsoft Security Blog
J
Java Code Geeks
Scott Helme
SecWiki News
C
CERT Recently Published Vulnerability Notes
Recorded Future
I
InfoQ
Security Archives - TechRepublic
Help Net Security
Cloudbric
C
Check Point Blog
Engineering at Meta
TaoSecurity Blog
B
Blog
Cyber Security Advisories - MS-ISAC
博
博客园_首页
N
News and Events Feed by Topic
云风的 BLOG
MyScale Blog
腾
腾讯CDC
量
量子位
Application and Cybersecurity Blog
K
Kaspersky official blog
Vercel News
F
Full Disclosure
T
Troy Hunt's Blog
Forbes - Security
S
Security @ Cisco Blogs
博客园 - 源码工作室
什么是公司战略
开通了知识星球圈“程序猿团队管理之道”
我们的双12活动总结
做电商的切身经验分享
Android编译报Errors running builder 'Android Pre Compiler' on project 'XXX' java.lang.NullPointerException
技术不如你,但老板就是赏识他,为什么?
看我是如何快速学习android开发的(一)
屌丝创业,你能闯过这么多关吗?
android不能显示log的问题
第一次和风投接触
你体会过创业的艰辛吗?
多个WPF工程公用ResourceDictionary文件
Bambook大赛程序结果发布兼拉票
Bambook大赛程序终于发布了
C# 如何检查当前用户在Windows系统下的权限
删除USB转串口的所有驱动
《家庭财务总管》升级(1.0.0.3)
MFC中Wizard中看不到类的解决办法
WPF中template的区别
总结C++中的所有强制转换函数(const_cast,reinterpret_cast,static_cast,dynamic_cast)(转载)
源码工作室
·
2009-03-17
·
via
博客园 - 源码工作室
标准c++中主要有四种强制转换类型运算符:
const_cast
,
reinterpret_cast
,
static_cast
,
dynamic_cast
等等。
1)
static_cast
<T*>(a)
将地址a转换成类型T,T和a必须是指针、引用、算术类型或枚举类型。
表达式
static_cast
<T*>(a), a的值转换为模板中指定的类型T。在运行时转换过程中,不进行类型检查来确保转换的安全性。
例子:
class
B { ... };
class
D :
public
B { ... };
void
f(B* pb, D* pd)
{
D* pd2 =
static_cast
<D*>(pb);
// 不安全, pb可能只是B的指针
B* pb2 =
static_cast
<B*>(pd);
// 安全的
...
}
class
B { ... };
class
D :
public
B { ... };
void
f(B* pb, D* pd)
{
D* pd2 =
static_cast
<D*>(pb);
// 不安全, pb可能只是B的指针
B* pb2 =
static_cast
<B*>(pd);
// 安全的
...
}
class
B { ... };
class
D :
public
B { ... };
void
f(B* pb, D* pd)
{
D* pd2 =
static_cast
<D*>(pb);
// 不安全, pb可能只是B的指针
B* pb2 =
static_cast
<B*>(pd);
// 安全的
...
}
2)
dynamic_cast
<T*>(a)
完成类层次结构中的提升。T必须是一个指针、引用或无类型的指针。a必须是决定一个指针或引用的表达式。
表达式
dynamic_cast
<T*>(a) 将a值转换为类型为T的对象指针。如果类型T不是a的某个基类型,该操作将返回一个空指针。
例子:
class
A { ... };
class
B { ... };
void
f()
{
A* pa =
new
A;
B* pb =
new
B;
void
* pv =
dynamic_cast
<A*>(pa);
// pv 现在指向了一个类型为A的对象
...
pv =
dynamic_cast
<B*>(pb);
// pv 现在指向了一个类型为B的对象
}
3)
const_cast
<T*>(a)
去掉类型中的常量,除了
const
或不稳定的变址数,T和a必须是相同的类型。
表达式
const_cast
<T*>(a)被用于从一个类中去除以下这些属性:
const
,
volatile
, 和 __unaligned。
例子:
class
A { ... };
void
f()
{
const
A *pa =
new
A;
//const对象
A *pb;
//非const对象
//pb = pa; // 这里将出错,不能将const对象指针赋值给非const对象
pb =
const_cast
<A*>(pa);
// 现在OK了
...
}
class
A { ... };
void
f()
{
const
A *pa =
new
A;
//const对象
A *pb;
//非const对象
//pb = pa; // 这里将出错,不能将const对象指针赋值给非const对象
pb =
const_cast
<A*>(pa);
// 现在OK了
...
}
class
A { ... };
void
f()
{
const
A *pa =
new
A;
//const对象
A *pb;
//非const对象
//pb = pa; // 这里将出错,不能将const对象指针赋值给非const对象
pb =
const_cast
<A*>(pa);
// 现在OK了
...
}
4)
reinterpret_cast
<T*>(a)
任何指针都可以转换成其它类型的指针,T必须是一个指针、引用、算术类型、指向函数的指针或指向一个类成员的指针。
表达式
reinterpret_cast
<T*>(a)能够用于诸如
char
* 到
int
*,或者One_class* 到 Unrelated_class*等类似这样的转换,因此可能是不安全的。
例子:
class
A { ... };
class
B { ... };
void
f()
{
A* pa =
new
A;
void
* pv =
reinterpret_cast
<A*>(pa);
// pv 现在指向了一个类型为B的对象,这可能是不安全的
...
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。
原文来自
— 版权归原作者所有。