const


title: const
date: 2021-06-19 10:22:11
tags: [C++]


const

const 含义

常类型是指使用类型修饰符 const 修饰的类型,常类型的变量、对象或指针的值是不能被更新的。

const 作用

  1. 可以定义常量

    const int a = 100;
    
  2. 类型检查

    const 定义的变量只有类型为整数或枚举,且以常量表达式初始化时才能作为常量表达式,其他情况下它只是一个 const 限定的变量,不要将与常量混淆。

  3. 防止修改,起保护作用,增加程序健壮性

    void f(const int i) {
    	i++; //error!
    }
    
  4. 可以节省空间,避免不必要的内存分配

    • const 定义常量从汇编的角度来看,只是给出了对应的内存地址,而不是像 #define 一样给出的是立即数。
    • const 定义的常量在程序运行过程中只有一份拷贝,而 #define 定义的常量在内存中有若干个拷贝。

const 对象默认为文件局部变量

非 const 变量默认为 extern。要使 const 变量能够在其他文件中访问,必须在文件中显式地指定它为 extern。

未被 const 修饰的变量在不同文件的访问

// file1.cpp
int ext;//会初始化为0

// file2.cpp
#include <iostream>
int main() {
	extern int ext;
	std::cout << (ext + 10) << std::endl;
}

const 常量在不同文件的访问

//extern_file1.cpp
extern const int ext = 12; // 常量在定义后就不能被修改,所以定义时必须初始化

//extern_file2.cpp
#include <iostream>
int main() {
    extern const int ext;
    std::cout << ext << std::endl;
}e

小结

可以发现未被 const 修饰的变量不需要 extern 显式声明。而 const 常量需要显式声明 extern,并且需要做初始化。因为常量在定义后就不能被修改,所以定义时必须初始化

指针与 const

与指针相关的 const 有四种:

const char * a; //指向 const 对象的指针或者说指向常量的指针。
char const * a; //同上
char * const a; //指向类型对象的 const 指针。或者说常指针、const 指针。
const char * const a; //指向 const 对象的 const 指针。
  • 如果 const 位于 * 的左侧,则 const 就是用来修饰指针所指向的变量,即指针指向为常量。
  • 如果 const 位于 * 的右侧,const 就是修饰指针本身,即指针本身是常量。
  • 即 const 有左修饰左,没左修饰右。(个人看法,有争议欢迎提出)

指向常量的指针

const int *ptr;
*ptr = 10; //error

ptr 是一个指向 const int 的指针,const 定义的是 int 类型,也就是 ptr 所指向的对象类型,而不是 ptr 本身,所以 ptr 可以不用赋初始值。但是不能通过 ptr 去修改所指对象的值。

不能使用 void 指针保存 const 对象的地址,必须使用 const void 类型的指针保存 const 对象的地址。**

const int p = 10;
const void* vp = &p;
void* vp = &p; //error

允许把非 const 对象的地址赋给指向 const 对象的指针

const int* ptr;
int val = 3;
ptr = &val; //ok

但这时不能通过 ptr 指针来修改 val 的值,即使它指向的是非 const 对象

我们不能使用指向 const 对象的指针修改基础对象,然而如果该指针指向了非 const 对象,可用其他方式修改其所指的对象。可以修改 const 指针所指向的值的,但是不能通过 const 对象指针来进行而已

int* ptr1 = &val;
*ptr1 = 4;
cout << *ptr << endl;
小结:
  1. 对于指向常量的指针,不能通过指针来修改对象的值。
  2. 不能使用 void* 指针保存 const 对象的地址,必须使用 const void* 类型的指针保存 const 对象的地址。
  3. 允许把非 const 对象的地址赋值给 const 对象的指针,如果要修改指针所指向的对象值,必须通过其他方式修改,不能直接通过当前指针直接修改。

常指针

const 指针必须进行初始化,且 const 指针的值不能修改。

#include<iostream>
using namespace std;
int main() {
    int num = 0;
    int* const ptr = &num; //const指针必须初始化,且const指针的值不能修改
    int* t = &num;
    *t = 1;
    cout << *ptr << endl;
    return 0;
}

上述修改 ptr 指针所指向的值,可以通过非 const 指针来修改。

最后,当把一个 const 常量的地址赋值给 ptr 时候,由于 ptr 指向的是一个变量,而不是 const 常量,所以会报错,出现:const int* -> int* 错误。

#include<iostream>
using namespace std;
int main() {
    const int num = 0;
    int* const ptr = &num; //error! const int* -> int*
    cout << *ptr << endl;
    return 0;
}

将改为 const int* ptr 改为const int* const ptr 即可修正错误。

指向常量的常指针

理解完前两种情况,下面这个情况就比较好理解了:

const int p = 3;
const int * const ptr = &p; //指向 const 对象的 const 指针。

ptr 是一个 const 指针,然后指向了一个 const int 对象。

函数中使用 const

const 修饰函数返回值

这个跟const修饰普通变量以及指针的含义基本相同

const int
const int func1();// 返回 const int 值。
const int*
const int* func2();// 指针指向的内容不变。
int *const
int *const func2();// 指针本身不可变。

const 修饰函数参数

传递过来的参数及指针本身在函数内不可变
void func(const int var); // 传递过来的参数不可变
void func(int *const var); // 指针本身不可变
参数指针所指内容为常量不可变
void StringCopy(char *dst, const char *src);

其中 src 是输入参数,dst 是输出参数。给 src 加上 const 修饰后,如果函数体内的语句试图改动 src 的内容,编译器将指出错误。这就是加了 const 的作用之一。

参数为引用,为了增加效率同时防止修改
void func(const A &a)

对于非内部数据类型的参数而言,像 void func(A a) 这样声明的函数注定效率比较低。因为函数体内将产生 A 类型的临时对象用于复制参数 a ,而临时对象的构造、复制、析构过程都将消耗时间。

为了提高效率,可以将函数声明改为 void func(A &a) ,因为引用传递仅借用一下参数的别名而已,不需要产生临时对象。

但是函数 void func(A &a) 存在一个缺点:引用传递有可能改变参数 a,这是我们不期望的。

解决这个问题很容易,加 const 修饰即可,因此函数最终成为 void func(const A &a)。

以此类推,是否应将 void func(int x) 改写为 void func(const int &x),以便提高效率?

完全没有必要,因为内部数据类型的参数不存在构造、析构的过程,而复制也非常快,值传递和引用传递的效率几乎相当。

小结
  1. 对于非内部数据类型的输入参数,应该将值传递的方式改为 const 引用传递,目的是提高效率。例如将 void func(A a) 改为 void func(const A &a)。
  2. 对于内部数据类型的输入参数,不要将值传递的方式改为 const 引用传递。否则既达不到提高效率的目的,又降低了函数的可理解性。例如 void func(int x) 不应该改为 void func(const int &x) 。

类中使用 const

在一个类中,任何不会修改数据成员的函数都应该声明为 const 类型。如果在编写 const 成员函数时,不慎修改数据成员,或者调用了其它非 const 成员函数,编译器将指出错误,这无疑会提高程序的健壮性。

//apple.cpp
class Apple
{
	......
    void take(int num) const;
	int add(int num) const;
    int getCount() const;
	......
};

使用 const 关键字进行说明的成员函数,称为常成员函数只有常成员函数才有资格操作常量或常对象,没有使用 const 关键字进行说明的成员函数不能用来操作常对象

类中的 const 成员变量必须通过初始化列表进行初始化

class Apple{
private:
    int people[100];
public:
    Apple(int i); 
    const int apple_number;
};
//类中的 const 成员变量必须通过初始化列表进行初始化
Apple::Apple(int i):apple_number(i)
{

}

const 对象只能访问 const 成员函数,而非 const 对象可以访问任意的成员函数,包括 const 成员函数

//apple.cpp
class Apple
{
private:
    int people[100];
public:
    Apple(int i); 
    const int apple_number;
    void take(int num) const;
    int add(int num);
	int add(int num) const;
    int getCount() const;

};

//main.cpp
#include<iostream>
#include"apple.cpp"
using namespace std;
Apple::Apple(int i):apple_number(i)
{

}
int Apple::add(int num){
    take(num);
}
int Apple::add(int num) const{
    take(num);
}
void Apple::take(int num) const
{
    cout<<"take func "<<num<<endl;
}
int Apple::getCount() const
{
    take(1);
//    add(); //error
    return apple_number;
}
int main(){
    Apple a(2);
    cout<<a.getCount()<<endl;
    a.add(10);
    const Apple b(3);
    b.add(100);
    return 0;
}

输出:

take func 1
2
take func 10
take func 100

上面 getCount() 方法中调用了一个 add() 方法,而 add 方法并非 const 修饰,所以运行报错。也就是说 const 对象只能访问 const 成员函数

而 add 方法又调用了 const 修饰的 take 方法,证明了非 const 对象可以访问任意的成员函数,包括 const 成员函数

除此之外,我们也看到 add 的一个重载函数,也输出了两个结果,说明 const 对象默认调用 const 成员函数

以上解决了两个面试问题:

  • 如果函数需要传入一个指针,是否需要为该指针加上 const,把 const 加在指针不同的位置有什么区别?
  • 如果写的函数需要传入的参数是一个复杂类型的实例,传值和传引用有什么区别,什么时候需要为传入的引用参数加上 const ?

PDF: 链接 密码:5ejb

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值