const用法
发表于:2024-12-08 | 分类: C++
字数统计: 1.2k | 阅读时长: 5分钟 | 阅读量:

const用法,指针、引用、函数、成员函数

const和普通变量

常量局部变量

  • 必须在声明时进行初始化
  • 不可直接修改
1
2
3
const int x = 10;  // 声明并初始化一个常量局部变量
// x = 20; // 错误:无法修改const变量的值
cout << "x = " << x << endl;

常量全局变量

与局部时一致

1
2
3
4
5
6
const int GLOBAL_CONST = 100;  // 全局常量变量

void example() {
// GLOBAL_CONST = 200; // 错误:无法修改const变量
cout << "GLOBAL_CONST = " << GLOBAL_CONST << endl;
}

const和引用

1
2
3
4
5
6
7
int main()
{
int a = 10;
const int &ref = a;
ref = 20; // 错误,不能修改常量
return 0;
}

const和指针

快速记忆,看是type*(可以改本身,不能改指向的值)还是*const(可以改指向的值,不能改本身)

常量指针

  • 指针指向的值不能更改
  • 指针本身可以修改(改变指向)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
const int x = 10;
// 指向常量的指针
const int *ptr = &x;
// *ptr = 20; // 错误:不能通过指针修改常量数据
cout << "*ptr = " << *ptr << endl;

// 指针本身可以修改
int y = 200;
ptr = &y;
cout << "*ptr = " << *ptr << endl;
return 0;
}

指针常量

和常量指针相反

  • 可以修改指针指向的值
  • 不可以修改指针本身
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
int value = 10;
// 指针常量
int *const ptr = &value;
// 可以修改指向的值
*ptr = 20;
cout << "*ptr = " << *ptr << endl;

// 不能修改指针本身
int value2 = 30;
// ptr = &value2; // 错误
return 0;
}

常量指针常量

指针和指向的值都不能更改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
int value = 10;
// 常量指针常量
const int *const ptr = &value;

// 指向的值和指针本身都不能修改
*ptr = 20; // 错误
int value2 = 30;
ptr = &value2; // 错误
return 0;
}

const与函数参数

const常常用来修饰函数的参数,表示该参数不可修改

基本使用

1
2
3
4
5
void example1(const int x)
{
x++; // 报错,x只读
cout << "x = " << x << endl;
}
1
2
3
4
5
void example2(const int &x)
{
x++; // 报错,x只读
cout << "x = " << x << endl;
}
1
2
3
4
5
void example1(const int *x)
{
*x = 20; // 错误,只读参数x
cout << "x = " << *x << endl;
}

结合指针

参考前面的coanst和指针

1
2
3
4
5
6
void example1(const int *x)
{
*x++; // 正确,优先级先计算x++,而常量指针可以改变指针指向,再计算*x,
cout << "x = " << x << endl;
cout << "x = " << *x << endl;
}
1
2
3
4
5
6
void example1(const int *x)
{
++*x; // 错误,优先级先计算++,只读不能修改
cout << "x = " << x << endl;
cout << "x = " << *x << endl;
}
1
2
3
4
5
6
7
void example1(const int *x)
{
*x++; // 正确,优先级先计算x++,而常量指针可以改变指针指向,再计算*x,
*x = 100; // 错误,常量指针不能改变指针指向的值
cout << "x = " << x << endl;
cout << "x = " << *x << endl;
}
1
2
3
4
5
6
void example1(const int *x)
{
(*x)++; // 错误,不能修改常量指针指向的值
cout << "x = " << x << endl;
cout << "x = " << *x << endl;
}
1
2
3
4
5
6
7
8
void example1(const int *const x)
{
// 错误,常量指针常量啥都不能改
*x++; // 错误
*x = 100; // 错误
cout << "x = " << x << endl;
cout << "x = " << *x << endl;
}

修饰成员函数

  • const在函数名称后面
  • 该成员函数不会修改类中除了被 mutable 修饰的成员

mutable关键字用于修饰类成员变量,使得该变量可以在常量成员函数中修改

myheader.cpp

MyClass

  • mutable x
  • y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include "myheader.h"

// 构造函数实现
MyClass::MyClass(int val) : x(val) {}

// 常量成员函数实现
void MyClass::display()
{
std::cout << "x = " << x << std::endl;
}

// 非常量成员函数实现
void MyClass::setX(int val)
{
x = val;
}

void MyClass::modX(int val, int val2) const
{
x = val; // 正确,有mutable修饰,可以修改成员变量的值
y = val2; // 报错,const修饰函数,且成员变量y没有mutable修饰
}

修饰函数返回值

  • 表示返回的值是常量,不允许修改。这通常用于返回指向常量的指针或引用。
  • 参考前面的const和常量,const和指针
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

// 返回引用类型的常量
const int &getConstant()
{
static int x = 10;
return x; // 返回对常量的引用
}

int main()
{
const int &ref = getConstant();
ref = 20; // 错误,不能修改常量
return 0;
}
上一篇:
C++数据类型补充
下一篇:
动态和静态内存分配选择