c++笔记——const限定符

const关键要点:

1、必须初始化

2、不能被重新赋值

3、不能将常量引用绑定到普通对象上,会报错

error: binding reference of type ‘int&’ to ‘const int’ discards qualifiers

4、可以将普通对象的常量引用赋值给普通对象

5、顶层和底层引用,以及和指针一起使用时,见下面的例子。

//
// Created by ht on 2023/4/19.
//

#ifndef TEST_THREAD_TEST_CONST_H
#define TEST_THREAD_TEST_CONST_H

#include <iostream>
#include <string>
#include <vector>
using namespace std;

void test_const(){
    int const a = 1;
    const int b = 2;
    //只读,不可重新赋值
    //a = b;  //error: assignment of read-only variable ‘a’
    //必须初始化
    //const int c;//error: uninitialized const ‘c’

    //初始化的时候用其他变量赋值
    int i = 42;
    const int ci = i;

    int j = ci;

    //const引用
    const int c2 = 1024;
    const int &c3 = c2;
    //int &c4 = c2;//error: binding reference of type ‘int&’ to ‘const int’ discards qualifiers
    const int &c5 = c3;
    //c5 = 42;//error: assignment of read-only reference ‘c5’

    int r1 = 42;
    const int &r2 = r1;
    int r3 = r2;//可以将r2赋值给一个非const变量
    cout << r3 << endl;
    //int &r4 = r2;//不可以赋值给一个引用变量

    const int &r5 = r2 * 2;


    //类型转换
    double dval = 3.14;
    const int &i1 = dval;//发生了类型转换,创建了一个临时变量const int temp = dval;
    cout << "i1 = " << i1 << endl;

    //发生了类型转换,创建了临时变量int temp = dval,而这个临时变量是个有右值,不能有引用类型
    //int &i2 = dval;//error: cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’

    //不发生类型的转换,i3绑定在变量dval上
    double &i3 = dval;

    //接下来对i1和i3分别修改内容,看dval是否发生变化
    i3 = 0;
    //i1 = 0;// error: assignment of read-only reference ‘i1’这里就能看出,i1不再是dval的引用,而是temp临时const变量的引用


    //指针和const
    const double pi = 3.14;
    //double *ptr = &pi;//error: invalid conversion from ‘const double*’ to ‘double*’
    const double *cptr = &pi;
   // *cptr = 42;//error: assignment of read-only location ‘* cptr’

   double d1 = 3.14;
   cptr = &d1;
   double *p2 = &d1;
   //*cptr = 1;//error: assignment of read-only location ‘* cptr’,只读
   *p2 = 2;

   //类型转换,报错都一样,不允许进行指针的类型转换,和是否有const限定无关
   //int *p3 = &d1;//error: cannot convert ‘double*’ to ‘int*’ in initialization
   //const int* p4 = &d1;//error: cannot convert ‘double*’ to ‘int*’ in initialization

   //const int* p5 = &pi;//error:cannot convert ‘const double*’ to ‘const int*’ in initialization


   //const指针,常量指针  从右往左读
   int errNumb = 0;
   int *const curErr = &errNumb;//离curErr最近的是const,表明curErr本身是常量对象,该对象的类型由声明符的其余部分确定。
   // 常量指针,指向int类型把变量

   const double dpi = 3.14159;
   const double *const dpip = &dpi;

   //试图改变常量指针
   int ai = 1024;
   int* iptr = &ai;
   //curErr = iptr;//error: assignment of read-only variable ‘curErr’
   *curErr = ai;//可以改变这个常量指针指向的数据

  // *dpip = 2.3;//error: assignment of read-only location ‘*(const double*)dpip’

  //int *const icptr;// error: uninitialized const ‘icptr’ 常量指针必须初始化

  //顶层const和底层const
  int i2 = 0;
  int * const pi1 = &i2;   //不允许改变pi1的值,顶层const
  const int ci2 = 42;//不允许改变ci2的值,顶层const
  const int* pci2 = &ci2;  //允许改变pci2的值,底层const
  const int* const cp3 = pci2;//靠右的是顶层const,靠左的是底层const
  const int &cir = ci2;//用于声明引用的const都是底层const


  //执行拷贝操作
  i2 = ci2;
  pci2 = pi1;

  //pi1 = cp3;//error: assignment of read-only variable ‘pi1’
  //int *p3 = cp3;//error: invalid conversion from ‘const int*’ to ‘int*’
  pci2 = cp3;
  pci2 = &i2;//int* 能转换成const int*


}


#endif //TEST_THREAD_TEST_CONST_H

 

posted @ 2023-04-20 16:17  水水滴答  阅读(275)  评论(0编辑  收藏  举报