const int、const int *、int *cosnt、const int * const、const int &的区别

本文详细解析了C/C++中const关键字的功能与用法,包括const int、const int*、int*const、const int* const及const int&的不同应用场景,强调了其在提升代码可读性和安全性方面的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:https://blog.youkuaiyun.com/itworld123/article/details/78967080

零、const的今世来生

    关键字又叫做限定符,因为它限定了声明的含义
    英文全称:constant,意义就是常数,故被const修饰的变量类型都是常类型,即不可改变。
    C中的const,在C90(ANSI C 或者 C89)中加入的关键字(之前有个K&R C )。
    C++中的const,暂时在网上没有找到相关介绍。由于C语言是在1972年至1973间创建的,而1990年在C90中加入了const,所以C++中的const大概是在其之后加入的。
    目的:取代#define。

一、const int

在定义变量的时候必须初始化,否则报错。

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    const int i = 0;
    //i = 4;             //error C3892: “i”: 不能给常量赋值

    //error C2734: “k”: 如果不是外部的,则必须初始化常量对象
    //const int k;       
    //k = 3;             //error C3892: “k”: 不能给常量赋值
    return 0;
}

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

二、const int *

1、声明该指针变量指向的是常量,即该指针变量的内容可以改变,但是该内容指向的内容不可改变!

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    const char *p = "Hello world!";
    //p[3] = '3';  //error C3892: “p”: 不能给常量赋值
    p = "Hi!";
    return 0;
}

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

2、虽然指向的内容不可改写,但是delete则是可以的。栗子:

    const int *p = new int[3];
    delete []p;
    p = nullptr;

    1
    2
    3

因为delete只是告诉系统,现在p所指向的这块内存已还给自由存储区,不再被使用,可以被其他程序改写。而此时p依旧指向这块不可使用的内存,故需要对p进行清零。
三、int *const

声明该指针变量为常变量,即指针变量里面的内容不可改变,但是该内容指向的内容可以改变!

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    char *const p = "Hello world!";
    p[3] = '3';  
    //p = "Hi!";  error C3892: “p”: 不能给常量赋值
    return 0;
}

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

四、const int * cosnt

声明该指针变量里面的内容不可改变,同时该内容指向的内容亦不可改变。

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    const char *const p = "Hello world!";
    //p[3] = '3';  //error C3892: “p”: 不能给常量赋值
    //p = "Hi!";   //error C3892: “p”: 不能给常量赋值
    return 0;
}

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

五、const int &

在引用前面加上const,代表该引用为常引用,即被引用的对象不可改变。若是在形参中使用,则不可达到在函数里面修改变量值的目的。

#include "stdafx.h"
#include <iostream>
using namespace std;

int add (const int &a,int &k)
{
    //a += 7;  //error C3892: “a”: 不能给常量赋值
    k += 8;
    return (a+k);
}

int _tmain(int argc, _TCHAR* argv[])
{
    int a = 5;
    int k = 9;
    const int &b = a;
    //b = 7;  //error C3892: “b”: 不能给常量赋值
    int t = add(a,k);
    cout<<"a = "<<a<<endl;
    cout<<"b = "<<b<<endl;
    cout<<"t = "<<t<<endl;
    cin.get();
    return 0;
}

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24

结果:
这里写图片描述

所以,如果想将形参作为函数的输出,同时使用了引用的方法,切记不要在引用前面使用const!
六、总结

以上总结了const的具体功能,其附加值也是很高的。
1、增加代码的可读性。可以明确向读你代码的人传递该形参、变量的可操作性。
2、合理的const可以让编译器帮你保护你的变量,即:减少bug的出现。
---------------------
作者:Ruo_Xiao
来源:优快云
原文:https://blog.youkuaiyun.com/itworld123/article/details/78967080
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值