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

推荐订阅源

Y
Y Combinator Blog
V
V2EX
Jina AI
Jina AI
爱范儿
爱范儿
M
MIT News - Artificial intelligence
量子位
L
LangChain Blog
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
腾讯CDC
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss

博客园 - eyoung

怎样才能把自己的网站做好(引用经济学理论知识)[转] 偷听城市 幻景 雾中的圣诞节 哎,怎么会是这样 征程 闭关 这般迷糊 聆听雨雪 记忆魔法--艾宾浩斯遗忘曲线[转] The life I desired 笑话背后的顿悟[转] 世事无常 这样软件开发人才级别的划分你同意吗[转] 起于C++ Hesitation! 十一流水账 程序员的桃花庵[转] GCC 概述
指针在typedef中使用后的理解
eyoung · 2006-12-28 · via 博客园 - eyoung

  在 typedef 中使用指针,通常会给后期的理解带来些困难,就此《C++ Primer》中也做了相应的解释,先前在看这部分时并不太理解,今天回过头来又看了一遍,仿佛有所领会,现在此介绍下自己的理解方法......

  示例代码:

typedef string *pstring;  // 声明pstring为字符串指针类型
const pstring cstr;

  很多人刚开始一般都不知道 cstr 变量是什么类型,有可能会误认为 cstr 的类型是:

const string *cstr;  // cstr是指向string类型const对象的指针

  其实可以这样理解,众所周知,当在声明一个普通的 const 变量时:

const int a;

  因为 int 前有 const 修饰,所以变量 a 被约定为 const 整型变量。

  同样的道理,当作如下声明时:

const pstring cstr;

  pstring前也有 const 修饰,由于前面已经声明了 pstring 为字符串指针类型(此处类似于 int 的状态),于是不难理解 cstr 是 const pstring 类型的变量:cstr 为指向 string 类型对象的 const 指针变量。

注:阅读 const 声明语句产生的部分问题,源于 const 限定符既可以放在类型前(出于惯例,大多如此),也可以放在类型后。
  鉴于此,如果这样声明:

pstring const cstr;  // 等同于声明:const pstring cstr;

  把 const 放在类型 pstring 之后,便会很清楚的知道 cstr 是指向 string 对象的 const 指针。

PS:由于水平有限,局限难免,若有高见,还请不吝言辞,多多指教!