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

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - dgz

c/c++之预处理 派生类的拷贝构造函数 虚函数和接口的差别 CALLBACK 函数 vc中LNK2001错误以及解决方案(转载) C++类对象的拷贝构造函数(转载) C++的static关键字(转载) 指针常量,常量指针 static 和 const的解释(转载) static用法小结(转载) Windows多线程多任务设计初步 最简单的完成端口代码 SOCKET模型之重叠I/O篇(转贴) WindowsSockets2.0:使用完成端口高性能,可扩展性Winsock服务程序(转载) CListCtrl 使用技巧(转载) C++字符串—— Win32 字符编码 com+调试 com+返回recordset 如何在VC++ 编写的组件中使用 ADO
数据类型转换:static_cast,const_cast等用法(转载)
dgz · 2007-04-13 · via 博客园 - dgz

* C++提供了四种新的类型强制:

static_cast
const_cast
reinterpret_cast
dynamic_cast

1)staic_cast静态强制;

不能在无关的指针之间进行static类型强制
class CAnimal
{
//...
public:
CAnimal(){}
};

class CGiraffe:public CAnimal
{
//...
public:
CGiraffe(){}
};

int main(void)
{
CAnimal an;
CGiraffe jean;

an = static_cast<CAnimal>(jean);//将对象jean强制成CAnimal类型
return 0;
}

2、const_cast类型强制

const_cast类型强制将一个const变量变成一个非const的等价形式
int main()
{
const int j = 99;
int * k;

k = const_cast<int *>(&j);//解除const
return 0;
}

3、reinterpret_cast运算符

reinterpret_cast运算符用来将一个类型指针转变为另一种类型的指针,也用在将整开型量转为指针,或将指针转为整型量上;
int main()
{
int j = 10;
int * ptr = &j;
char * cptr;

cptr = reinterpret_cast<char *>(ptr);//将int指针类型转变为char的指针类型

return 0;
}

4、dynamic_cast运算符

dynamic_cast的主要目的是:

1)它返回派生类对象的地址;
2)它测试基类指针是否指向下一尖括号<>中所指定类型的对象

dynamic_cast是一个运行时类型信息,dynamic_cast运算符将指向派生对象的基类部分的基类指针转变为指向派生对象的派生类指针,dynamic_cast必须严格地指定与派生对象相同的类,或者它返回NULL指针;
class CAnimal
{
//...
};
class CGiraffe:public CAnimal
{
//...
};
class CGoat:public CAnimal
{
//...
};

int main()
{
CGiraffe gene;
CAnimal * aptr = &gene;
CGiraffe * ptr1,* ptr2;

ptr1 = dynamic_cast<CGiraffe *>(aptr);
ptr2 = dynamic_cast<CGoat *>(aptr); //return NULL

return 0;
}