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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - BearOcean

LOG.ZS.0001.基于Freetype的游戏字体渲染优化思路 C++ 下啥时候用struct, 啥时候用class C++ 和 Java 中的变参 解决站点关键数据,状态数据,无须持久化数据的一些思路 - BearOcean BS程序代码与安全与基本攻击/防御模式 Struts 实现的I18N Ant 阅读笔记 进度,效率,与个人事务管理 Personal Task 1.0 MySql与Java的时间类型 数据挖掘概述 解决Thread 的关闭问题和参数传递时想到的办法. Command 模式 .Net标准控件与自定义控件(2) ToolTipButton 内网聊天工具FreeChat 2.0 FreeChat 2.0 ...大改 模型和架构 局域网聊天工具FreeChat 1.0 开发日志 - BearOcean 内网聊天工具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 为啥放左边表示修饰对象的时候想起这个故事来着。

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