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

推荐订阅源

人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
L
LINUX DO - 最新话题
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
S
Schneier on Security
B
Blog
The Register - Security
The Register - Security
SecWiki News
SecWiki News
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security Affairs
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
T
Tenable Blog
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
D
DataBreaches.Net
S
Secure Thoughts
Security Latest
Security Latest
H
Heimdal Security Blog
The Hacker News
The Hacker News
O
OpenAI News
AWS News Blog
AWS News Blog
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
腾讯CDC
U
Unit 42
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Hugging Face - Blog
Hugging Face - Blog
The Last Watchdog
The Last Watchdog
Recorded Future
Recorded Future
V2EX - 技术
V2EX - 技术
爱范儿
爱范儿
F
Full Disclosure

博客园 - BearOcean

LOG.ZS.0001.基于Freetype的游戏字体渲染优化思路 C++ 下啥时候用struct, 啥时候用class C++ 和 Java 中的变参 解决站点关键数据,状态数据,无须持久化数据的一些思路 BS程序代码与安全与基本攻击/防御模式 Struts 实现的I18N Ant 阅读笔记 进度,效率,与个人事务管理 Personal Task 1.0 MySql与Java的时间类型 数据挖掘概述 解决Thread 的关闭问题和参数传递时想到的办法. Command 模式 .Net标准控件与自定义控件(2) ToolTipButton 内网聊天工具FreeChat 2.0 FreeChat 2.0 ...大改 模型和架构 局域网聊天工具FreeChat 1.0 开发日志 内网聊天工具FreeChat Beta 为Socket写的附加方法 .Net 事件
const 和指针
BearOcean · 2015-02-23 · via 博客园 - BearOcean

c++用了那么久,觉得 const 和指针配合到一起的时候就会有点点分不出来。

如下:

const Data* pData;

Data const * pData

Data * const pData

const Data * const pData      

Data const * const pData

是不是有点晕?

我其实用得最多的是 const Data* pData, 也理解该语句是定义 pData指向的对象是不允许修改的(不能通过pData指针调用非const方法)。

那么Data * const pData 实际就是指针本身是不可修改的(你不能将该指针赋值成另一个地址)。

实际上只会出现3中情况.

一种修饰语义是将指针所指向的对象修饰为const.

一种修饰予以是将指针本身(值类型)修饰为const, 本质上像你定义一个 const long 一样。

最后一种是对象为const 同时指针也为const.

那么,上面那一串代码都可以对号入座。

简单的办法是:

const 关键字出现在 * 前面, 修饰的是对象

const 关键字出现在 * 后面, 修饰的是指针

好了,再分析之前的例子:

const Data* pData;               //修饰对象

Data const * pData               //修饰对象

Data * const pData               //修饰指针

const Data * const pData              //修饰对象 + 指针

Data const * const pData              //修饰对象 + 指针

所以有那么多种写法,其实就是在*左边和右边的问题。

我们应该始终按自己的风格选择一种固定模式就好。

为什么const 出现在 * 左边是修饰对象而不是修饰指针

而为什么const 出现在 * 右边是修饰指针而不是修饰对象

要理解这个,需要先理解神射手理论

说有一个神射手,随意的在枪靶上,每隔1cm的地方打了一个眼。

这个枪靶上居住着一种二维生物。

经过很多该生物的宇宙年后,该生物中的科学家终于发明了太空飞船,开始探索自己的宇宙。

最后他们总结出他们的宇宙第一定律,就是每隔1cm的地方有个洞。

哈哈, 我忘记我是在哪看到这个段子的了,是3T还是BigBang. 但是我在思考const 为啥放左边表示修饰对象的时候想起这个故事来着。

不要在意这些细节,我真是一个很会自娱自乐的人。