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

推荐订阅源

The GitHub Blog
The GitHub Blog
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
有赞技术团队
有赞技术团队
V
Visual Studio Blog
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿
Vercel News
Vercel News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
D
DataBreaches.Net
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
大猫的无限游戏
大猫的无限游戏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
The Cloudflare Blog
宝玉的分享
宝玉的分享
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 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:由于水平有限,局限难免,若有高见,还请不吝言辞,多多指教!