typedef?

u013628777 2014-05-20 05:07:13
《C++Primer》第四版
4.2.5 指针和const限定符
4指针和typedef
建议:理解复杂的const类型的声明

string s;
typedef string *pstring;
const pstring cstr1=&s;




string s;
typedef string *pstring;
pstring const cstr2=&s;


问题:
上面两个代码,都是初始化const指针,相当于:
string * const cstr1=&s;
string * const cstr2=&s;

对么?
...全文
210 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2014-05-22
  • 打赏
  • 举报
回复
非常量指针类型(别名),和常量指针类型(别名),只能用两个不同的 typedef 分别独立定义类型(别名)。
lm_whales 2014-05-22
  • 打赏
  • 举报
回复
typedef  const_ptr_type const  thrd_cons_ptr_const_type;                  // 第三方式的常量指针常量
lm_whales 2014-05-22
  • 打赏
  • 举报
回复
typedef  string RawType;       //某个原始数据类型 ,这里是 string,为原始数据类型定义别名RawType

typedef RawType elem_type;                             //为原始数据类型定义别名elem_type
typedef const RawType const_elem_type;            //常量原始数据类型别名 const_elem_type
typedef RawType const sec_const_elem_type;   //第二种方式定义的,常量原始数据类型sec_const_elem_type
typedef elem_type const thrd_const_elem_type;   //第三种(?)方式定义的,常量原始数据类型thrd_const_elem_type
//以下省略类型二字
typedef RawType& ref_type;                          // 引用 
typedef const RawType& const_ref_type;             // 常量引用

typedef RawType* ptr_type;                         //指针
typedef const RawType* const_ptr_type;            //常量指针

typedef  RawType *const  ptr_const_type;            //指针常量
typedef const RawType *const  const_ptr_const_type; //常量指针常量

typedef  const  ptr_type  sec_ptr_const_type;           //第二方式的指针常量
typedef  ptr_type const  thrd_ptr_const_type;          // 第三方式的指针常量     

typedef  const  const_ptr_type  sec_const_ptr_const_type;     //第二方式的常量指针常量
typedef  const_ptr_type const  thrd_ptr_const_type;                  // 第三方式的常量指针常量
PS: 一个typedef 定义的非常量指针类型,不能再次用typedef 定义出常量指针类型 非常量指针,和常量指针,只能用两个不同的 typedef 分别独立定义。 一个typedef 定义的非常量指针类型(别名) 不能用于,定义常量指针对象(数据),没有任何直接的方式可以这样做。
time_exceed 2014-05-21
  • 打赏
  • 举报
回复
引用 5 楼 lm_whales 的回复:
typedef 符号,不具有分割性。 该符号,被组合成一个完整的类型,不在分别解释。
正解,typedef XX XXX和XX typedef XXX是一样的
u013628777 2014-05-21
  • 打赏
  • 举报
回复
各位: 怎么样写typedef? 替换后,成为: string const *cstr=&s;
u013628777 2014-05-21
  • 打赏
  • 举报
回复
各位: 12楼问题?
大松哥 2014-05-20
  • 打赏
  • 举报
回复
引用 7 楼 rmaly 的回复:
两个不一样的,告诉你一个技巧,*在const前的表示为常量指针,*在const后的表示指向常量的指针
七楼正解呀,常量指针是指该指针的指不可改变,指向常量的指针是指指针指向的内存空间不可修改
u013628777 2014-05-20
  • 打赏
  • 举报
回复
各位: string const *cstr=&s; 上面const对象,怎样写typedef?
u013628777 2014-05-20
  • 打赏
  • 举报
回复
各位: string const *cstr=&s
u013628777 2014-05-20
  • 打赏
  • 举报
回复
楼上: const对象, 怎样写typedef?
ForestDB 2014-05-20
  • 打赏
  • 举报
回复
1. 对 2. typedef const string * pcstring;
u013628777 2014-05-20
  • 打赏
  • 举报
回复
lm_whales: 对! 再说说2楼问题?
rmaly 2014-05-20
  • 打赏
  • 举报
回复
两个不一样的,告诉你一个技巧,*在const前的表示为常量指针,*在const后的表示指向常量的指针
u013628777 2014-05-20
  • 打赏
  • 举报
回复
楼上各位: 2楼问题?
lm_whales 2014-05-20
  • 打赏
  • 举报
回复
typedef 符号,不具有分割性。 该符号,被组合成一个完整的类型,不在分别解释。
碼上道 2014-05-20
  • 打赏
  • 举报
回复
引用 楼主 u013628777 的回复:
《C++Primer》第四版 4.2.5 指针和const限定符 4指针和typedef 建议:理解复杂的const类型的声明

string s;
typedef string *pstring;
const pstring cstr1=&s;

string s;
typedef string *pstring;
pstring  const cstr2=&s;
问题: 上面两个代码,都是初始化const指针,相当于: string * const cstr1=&s; string * const cstr2=&s; 对么?
是的,适当使用const还是有好处的
Saleayas 2014-05-20
  • 打赏
  • 举报
回复
const 放在类型前后都是一致的,我喜欢放在类型的后面。
u013628777 2014-05-20
  • 打赏
  • 举报
回复
1.上面两个都是const指针。对么? 2.怎样,用typedef定义const对象的指针(指向string类型const对象的指针)?
赵4老师 2014-05-20
  • 打赏
  • 举报
回复
不要纠结各种常量了,这个世界上唯一不变的就是变化。用API WriteProcessMemory还能修改正运行的其它进程的内存里面的所谓常量呢!

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧