• C++const修饰指针时类型识别方法


    多才多艺的const,修饰指针的的功能非常难辨认,总结出一个方法:用类似于函数的方式记忆

    1、const修饰函数【这种形式非常好识别】

    const int* getData();    //返回值为常量[指向的对象]
    int getData() const;     //函数本身为“常量”[函数不能修改类属性]

    可以看到函数对象[getData]左边的const用于定义返回值为常量;

    函数对象右边的const定义函数本身为常量。

    2、根据const修饰函数模式去理解const修饰指针

    假设类型为T,T*就是声明一个T类型的指针,当加上const一起工作时:

    T*左边的const代表返回值也就是指向的值为const;

    T*右边的const代表指针本身是const;

    T*左右两边都有const代表值和指针都是const。

    代码:

    char greeting[] = "hello";
    char* p = greeting;             //non-constpointer,non-const data
    const char* p  =  greeting;     //non-constpointer,const data
    char* const p = greeting;       //constpointer,non-const data
    const char* const p = greeting; //constpointer,const data

     




    长风破浪会有时,直挂云帆济沧海!
    可通过下方链接找到博主
    https://www.cnblogs.com/judes/p/10875138.html
  • 相关阅读:
    将博客搬至CSDN
    05 Python字符串的通用操作
    02 Shell变量
    01 Shell脚本编程入门知识
    windows10安装Python环境
    03 Python数值类型及数字类型详解
    02 变量和语句
    01 交互解释器
    poi.jar处理excel表
    (41)java并发包中的线程池种类及特性介绍
  • 原文地址:https://www.cnblogs.com/judes/p/15779996.html
Copyright © 2020-2023  润新知