zoukankan      html  css  js  c++  java
  • C++之const关键字

    const关键字——用来控制变量是否可以变化。

    const与指针类型

    1、说一个比较好记的方法来区分 int const *p与 int* const p,把*读作pointer to然后从后往前读。

    第一个int const *p就可以读作 p is a pointer to const int,p是指向常量的指针

    第二个int* const p就可以读作 p is a const pointer to int,p是指向int型的常指针

    2、const指针主要看const修饰p还是*p,修饰p不能改变p的指向(不能使p指向其他变量),修饰*p不能改变*p的值(即p指向的变量的值)

    3、把const读作常量,把*读做指针,按顺序翻译,来试试

    const int * p; //常量指针p

    int const * p; //常量指针p

    int * const p; //指针常量p

    指针常量和常量指针:

    <1>. 指针常量,指针本身就是个常量 ,所以不能重新指向,int * const p,const修饰的是p(即指针本身)

    <2>. 常量指针,指向常量的指针,本身是普通指针,所以可以重新指向,但不能通过*p重新赋值,int const *p,const修饰的是*p(即指针指向的变量)

  • 相关阅读:
    Jersey(1.19.1)
    Jersey(1.19.1)
    Jersey(1.19.1)
    Jersey(1.19.1)
    Jersey(1.19.1)
    Jersey(1.19.1)
    Jersey(1.19.1)
    Jersey(1.19.1)
    17. Letter Combinations of a Phone Number
    37.Sudoku Solver
  • 原文地址:https://www.cnblogs.com/Tang-tangt/p/9579727.html
Copyright © 2011-2022 走看看