C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++ 指针常量、常量指针与常量指针常量

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

作者:司徒轩宇

本文深入解析了C++中指针与const的组合使用,包括指针常量、常量指针和常量指针常量三种类型,下面就拉介绍一下这三种的使用与区别,具有一定的参考价值,感兴趣的可以了解一下

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

什么是const关键字?

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

1. 指针常量(Pointer Constant)

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

声明语法

type* const ptr = 初始地址;

代码示例

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 20;
    int* const ptr = &a;  // 必须在声明时初始化
    
    cout << "初始值: a = " << a << endl;
    
    *ptr = 30;  // ✅ 正确:可以修改指向的值
    cout << "修改后: a = " << a << endl;
    
    // ptr = &b;  // ❌ 错误:不能改变指针的指向
    // 编译错误:error: assignment of read-only variable 'ptr'
    
    return 0;
}

特点总结

2. 常量指针(Pointer to Constant)

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

声明语法

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

代码示例

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 20;
    const int* 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;
    
    return 0;
}

特点总结

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

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

声明语法

const type* const ptr = 初始地址;

代码示例

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 20;
    const int* const ptr = &a;
    
    cout << "a = " << a << ", *ptr = " << *ptr << endl;
    
    // *ptr = 30;  // ❌ 错误:不能修改指向的值
    // ptr = &b;   // ❌ 错误:不能改变指针的指向
    
    // 但可以直接修改变量本身(前提是变量不是const)
    a = 40;
    cout << "直接修改a后: *ptr = " << *ptr << endl;
    
    return 0;
}

特点总结

快速记忆技巧

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

const int* ptr1;    // const在*左边 → 常量指针(指向常量)
int const* ptr2;    // const在*左边 → 常量指针(指向常量)
int* const ptr3;    // const在*右边 → 指针常量(指针是常量)
const int* const ptr4; // 两边都有const → 常量指针常量

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

方法二:从右向左读法

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

实际应用场景

1. 函数参数中的使用

// 常量指针:保护数据不被意外修改
void printArray(const int* arr, int size) {
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
        // arr[i] = 0;  // ❌ 错误:不能修改
    }
    cout << endl;
}

// 指针常量:固定操作某个对象
void incrementValue(int* const ptr) {
    *ptr += 1;  // ✅ 可以修改值
    // ptr = nullptr;  // ❌ 错误:不能改变指向
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int value = 10;
    
    printArray(arr, 5);
    incrementValue(&value);
    cout << "value = " << value << endl;
    
    return 0;
}

2. 字符串处理

#include <iostream>
#include <cstring>
using namespace std;

int main() {
    char str1[] = "Hello";
    char str2[] = "World";
    
    // 常量指针:指向字符串常量
    const char* message = "Immutable string";
    // *message = 'h';  // ❌ 错误:字符串常量不可修改
    
    // 指针常量:固定指向某个字符串
    char* const fixedPtr = str1;
    fixedPtr[0] = 'h';  // ✅ 可以修改内容
    // fixedPtr = str2;  // ❌ 错误:不能改变指向
    
    cout << message << endl;
    cout << fixedPtr << endl;
    
    return 0;
}

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

class MyClass {
private:
    int data;
public:
    MyClass(int d) : data(d) {}
    
    // 常量成员函数:不能修改成员变量
    int getData() const {
        return data;
    }
    
    void setData(int d) {
        data = d;
    }
};

int main() {
    MyClass obj(100);
    const MyClass constObj(200);
    
    // 指向常量的指针
    const MyClass* ptr1 = &constObj;
    cout << ptr1->getData() << endl;  // ✅ 可以调用const成员函数
    // ptr1->setData(300);  // ❌ 错误:不能调用非const成员函数
    
    // 指针常量
    MyClass* const ptr2 = &obj;
    ptr2->setData(150);  // ✅ 可以修改对象
    // ptr2 = &constObj;  // ❌ 错误:不能改变指向
    
    return 0;
}

到此这篇关于C++中的指针常量、常量指针与常量指针常量详解的文章就介绍到这了,更多相关C++ 指针常量、常量指针与常量指针常量内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文