【C++ Primer学习笔记1】const 小结

1const int i = 5 在编译时编译器如define方法处理,若需要作用于其他文件应加extern 改为externconst int

 

2reference to const : const int &rtoc= const int odata

 

   int i = 40;

   const int &r1 = i;      //允许将const int&绑定在一个普通int

   const int &r2 = 40 * 2;//同样合法,40字面值为int

   int &r3 = r1;           //错误

 

3、常量引用可能引用一个非常量的值(参数列表)

 

4pointer to const

 

   const double pi = 3.14D;

   double p* = π        //Wrong!

   const double *cp = π //Right

   double dval = 3.14;

   cp = &dval;             //Right

 

5const pointer

 

   int i = 10;

   int *const curi = &i;

  

   const double pi = 3.14159;

   const double *const pip = π//constpointer to const常量常指针

 

6top-level const and low-level const

  

   top-level const : object is a const(class orobject or pointer);

   low-level const : pointer or reference tosth;

  

   执行拷贝或赋值操作时须有相同的底层const资格

 

   int i = 0;

   int *const p1 = &i;    //top

   const int ci = 40;     //top

   const int *p2 = &ci;   //low

 

7、指针、常量和类型别名

 

   typedef char *pstring;

   const pstring cstr = 0; // const pointer tochar

   const pstring *ps;      // pointer to const pointer to char

   其中typedef不能理解为填充替换含义,pstring 的基本数据类型是指针 , char* 的基本数据类型为char

   const pstring cstr = 0;//指向charconst pointer

   const char *cstr = 0;  //指向const charpointer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值