有关const的问题

mn_th 2002-05-02 01:07:13
//??因为我不想输出的指针所指向的内容在外部被修改(此处必须把内部指针暴露),所以输出为const * const,
//这样就要求调用时也必须用const * const,这太麻烦了,有没有好的解决办法
...全文
49 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jamesfangjing 2002-05-02
  • 打赏
  • 举报
回复
const *就可以了吧!
ajoo 2002-05-02
  • 打赏
  • 举报
回复
normally I'll write it in this form:
const X * const
the first const, sice it's closer to X, it means the X pointed to by the pointer cannot be changed.
The second const, since it's closer to the *, it means the pointer variable itself cannot be changed to point to some other address.

Sometimes you need one of them, sometimes you need both, it depends on your requirement.

For example, when writing a function return type, the second const is useless.
albertjin123 2002-05-02
  • 打赏
  • 举报
回复
后置的const 更方便,比如,

CSome oSome ;
...
CSome const * const & p = &oSome ;
zyf_zsj1 2002-05-02
  • 打赏
  • 举报
回复
可以在前面用default定义const
Last_Dodo 2002-05-02
  • 打赏
  • 举报
回复
其实const X *加行了因为你只要求指针指向的对象不能被改变。
albertjin123 2002-05-02
  • 打赏
  • 举报
回复
to ajoo(jet pig),
I think it's a good style to write:
int const i = 10 ;
int const * const pI = &i ;

Don't you think it's clearer?
ajoo 2002-05-02
  • 打赏
  • 举报
回复
what is const * const?
should be const xxx*

and avoid using cast in all time.
albertjin123 2002-05-02
  • 打赏
  • 举报
回复
const_cast 可以帮你去除const

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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