当前位置: 首页 > news >正文

C++中的指针常量、常量指针与常量指针常量详解

在C++编程中,指针和const关键字的组合使用是每个开发者必须掌握的重要概念。正确理解这些概念不仅能帮助我们编写更安全的代码,还能提升代码的可读性和维护性。本文将深入探讨三种常见的指针与const组合:指针常量、常量指针和常量指针常量。

什么是const关键字?

在深入讨论之前,我们先简单回顾一下const关键字的作用。const用于定义常量,表示被修饰的变量或对象不可修改。当const与指针结合时,根据其位置的不同,会产生不同的语义。

1. 指针常量(Pointer Constant)

指针常量指的是指针本身是常量,即指针的指向(内存地址)不可改变,但可以通过指针修改指向的值。

声明语法

1

type*constptr = 初始地址;

代码示例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

#include <iostream>

usingnamespacestd;

intmain() {

inta = 10, b = 20;

int*constptr = &a;// 必须在声明时初始化

cout <<"初始值: a = "<< a << endl;

*ptr = 30;// ✅ 正确:可以修改指向的值

cout <<"修改后: a = "<< a << endl;

// ptr = &b; // ❌ 错误:不能改变指针的指向

// 编译错误:error: assignment of read-only variable 'ptr'

return0;

}

特点总结

  • 指针的指向固定,不可改变
  • 可以通过指针修改指向的值
  • 必须在声明时初始化

2. 常量指针(Pointer to Constant)

常量指针指的是指向的值是常量,不能通过指针修改指向的值,但可以改变指针的指向。

声明语法

1

consttype* ptr;// 或 type const* ptr;

代码示例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

#include <iostream>

usingnamespacestd;

intmain() {

inta = 10, b = 20;

constint* ptr = &a;

cout <<"a = "<< a <<", *ptr = "<< *ptr << endl;

ptr = &b;// ✅ 正确:可以改变指向

cout <<"现在指向b: *ptr = "<< *ptr << endl;

// *ptr = 30; // ❌ 错误:不能修改指向的值

// 编译错误:error: assignment of read-only location '*ptr'

// 但可以直接修改变量本身

b = 40;

cout <<"直接修改b后: *ptr = "<< *ptr << endl;

return0;

}

特点总结

  • 不能通过指针修改指向的值
  • 可以改变指针的指向
  • 不要求在声明时初始化

3. 常量指针常量(Constant Pointer to Constant)

常量指针常量是指针本身和指向的值都是常量,既不能改变指针的指向,也不能通过指针修改指向的值。

声明语法

1

consttype*constptr = 初始地址;

代码示例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

#include <iostream>

usingnamespacestd;

intmain() {

inta = 10, b = 20;

constint*constptr = &a;

cout <<"a = "<< a <<", *ptr = "<< *ptr << endl;

// *ptr = 30; // ❌ 错误:不能修改指向的值

// ptr = &b; // ❌ 错误:不能改变指针的指向

// 但可以直接修改变量本身(前提是变量不是const)

a = 40;

cout <<"直接修改a后: *ptr = "<< *ptr << endl;

return0;

}

特点总结

  • 指针的指向固定,不可改变
  • 不能通过指针修改指向的值
  • 必须在声明时初始化

快速记忆技巧

方法一:看const相对于*的位置

1

2

3

4

constint* ptr1;// const在*左边 → 常量指针(指向常量)

intconst* ptr2;// const在*左边 → 常量指针(指向常量)

int*constptr3;// const在*右边 → 指针常量(指针是常量)

constint*constptr4;// 两边都有const → 常量指针常量

口诀:“左定值,右定向,两边定都定”

  • const在*左边:指向的值是常量
  • const在*右边:指针本身是常量
  • 两边都有const:都不可变

方法二:从右向左读法

1

2

3

int*constptr;// ptr is a const pointer to int

constint* ptr;// ptr is a pointer to const int

constint*constptr;// ptr is a const pointer to const int

实际应用场景

1. 函数参数中的使用

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

// 常量指针:保护数据不被意外修改

voidprintArray(constint* arr,intsize) {

for(inti = 0; i < size; i++) {

cout << arr[i] <<" ";

// arr[i] = 0; // ❌ 错误:不能修改

}

cout << endl;

}

// 指针常量:固定操作某个对象

voidincrementValue(int*constptr) {

*ptr += 1;// ✅ 可以修改值

// ptr = nullptr; // ❌ 错误:不能改变指向

}

intmain() {

intarr[] = {1, 2, 3, 4, 5};

intvalue = 10;

printArray(arr, 5);

incrementValue(&value);

cout <<"value = "<< value << endl;

return0;

}

2. 字符串处理

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

#include <iostream>

#include <cstring>

usingnamespacestd;

intmain() {

charstr1[] ="Hello";

charstr2[] ="World";

// 常量指针:指向字符串常量

constchar* message ="Immutable string";

// *message = 'h'; // ❌ 错误:字符串常量不可修改

// 指针常量:固定指向某个字符串

char*constfixedPtr = str1;

fixedPtr[0] ='h';// ✅ 可以修改内容

// fixedPtr = str2; // ❌ 错误:不能改变指向

cout << message << endl;

cout << fixedPtr << endl;

return0;

}

3. 面向对象编程中的应用

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

classMyClass {

private:

intdata;

public:

MyClass(intd) : data(d) {}

// 常量成员函数:不能修改成员变量

intgetData()const{

returndata;

}

voidsetData(intd) {

data = d;

}

};

intmain() {

MyClass obj(100);

constMyClass constObj(200);

// 指向常量的指针

constMyClass* ptr1 = &constObj;

cout << ptr1->getData() << endl;// ✅ 可以调用const成员函数

// ptr1->setData(300); // ❌ 错误:不能调用非const成员函数

// 指针常量

MyClass*constptr2 = &obj;

ptr2->setData(150);// ✅ 可以修改对象

// ptr2 = &constObj; // ❌ 错误:不能改变指向

return0;

}

到此这篇关于C++中的指针常量、常量指针与常量指针常量详解的文章就介绍到这了

http://www.jsqmd.com/news/919814/

相关文章:

  • Proxmox VE存储规划避坑指南:为什么你的local目录总是不够用?从分区到LVM的深度解析
  • 2026年生产线推荐供应商品牌排名,瑞德佑业在列 - mypinpai
  • 健身器材十大品牌综合盘点
  • 从UDS诊断失败案例复盘:深入理解ISO 15765协议中的流控与超时机制
  • MATLAB数字全息三算法实现包:菲涅尔积分、卷积衍射与角谱传播
  • STL转STEP格式转换器:5分钟掌握CAD工程文件无缝转换技术
  • 如何通过脑的识别加强AI与用户的黏度?
  • 新手入门电子焊接:从零组装STC单片机红蓝警车模型
  • 调参玄学?ESN储备池的谱半径、稀疏度到底怎么设?一份基于Numpy的实验报告
  • 2026年杭州屋面翻新管理团队实力TOP10排行:杭州外立面翻新改造/杭州屋面渗漏治理/杭州屋面漏水维修/杭州屋面维修/选择指南 - 优质品牌商家
  • 抖音无水印下载器终极指南:3分钟学会下载纯净短视频
  • 用Python和Pandas玩转全球凋落物数据集:从ORNL DAAC下载到物候分析实战
  • Nginx UI单点登录配置终极指南:3种方式告别重复登录烦恼
  • 2026年金华旧设备回收服务商评测:义乌,东阳,兰溪,金华收破烂上门回收电话/合规与效率双维度 - 优质品牌商家
  • 【RT-DETR实战】094、无人机视角(UAV)目标检测改进实战:当RT-DETR遇上高空小目标
  • 8051双数据指针编译器支持与优化实践
  • 重庆茅台酒上门回收靠谱判定标准与实操推荐 - 优质品牌商家
  • C++中指针变量的使用指南
  • 2026年山东刺绣贴排行榜,亲测分享实践心得
  • 使用 iNaturalist.org 的 OF (Observation Field 观察字段) 的注意事项
  • 2026年好用的打磨抛光品牌商排名,靠谱的在这里 - mypinpai
  • AMD Ryzen处理器深度调试指南:5个SMU系统管理单元优化实战技巧
  • 3步搞定MOOC课程离线下载:免费建立个人学习资源库
  • Lindy下一代架构选型尘埃落定?4大备选方案终局分析,附迁移成本测算表(限前500名领取)
  • 避开这些坑!基因家族染色体位置分析中GFF文件与基因ID匹配的常见错误
  • Windows凭据窃取技术:从SAM数据库提取密码哈希
  • 别再用MLP了?KAN模型实战:用Python复现论文核心,实测速度到底慢多少
  • 【分享】种子磁力下载器1.7.2 解锁年费会员 不限速下载
  • AI编程8:百度的Baidu Comate(文心快码)
  • claude code 消息系统 Skill 调用(五)