zoukankan      html  css  js  c++  java
  • const

    1. 指针指向的内容不可以修改

    以下两种写法等价

    const int *p1;
    int const * p2;
    
    p1 = new int[4];
    p2 = new int[5];
    
    p1[0] = 4;    // Error 
    p2[0] = 4;    // Error

    2. 指针不可以修改

    既指针初始化以后,不可以指向其他的地址;但是指针指向的内容可以修改

    int * const p0; // Error, we must init const pointer
    int * const p1 = nullptr;
    int * const p2 = new int[4];
    p2[0]=4;
    
    p2 = new int[5]; // Error, we cann't change p2

    3. 指针与指针指向的内容都不可以修改

    以下两种方式等价。

    const int * const p1 = nullptr;
    int const * const p2 = new int[4];

     4. const  引用

    int a = 5;
    const int & inf1 = a;
    int const & inf2 = a;
    
    inf1 = 10; // Error
    inf2 = 100; // Error
  • 相关阅读:
    Meet Hadoop
    C++常用函数
    Summary
    获得小黄衫感想
    课程作业(八)
    课程作业(七)
    课程作业(六)
    课程作业(五)
    课程作业(四)
    课程作业(三)
  • 原文地址:https://www.cnblogs.com/ordili/p/10342043.html
Copyright © 2011-2022 走看看