Featured image of post 【C++ 基础进阶】 Const

【C++ 基础进阶】 Const

const 用法

|
4430 字
|

Const 用法

本博客参照:CPlusPlusThings
加上了一些自己的理解。


1. const 含义

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


2. const 作用

  • 定义常量

    1
    
    const int a = 100;
    
  • 类型检查

    • const 常量支持所有类型
    • 其他情况下它只是一个 const 限定的变量,不要将与常量混淆。
  • 防止修改,起保护作用,增加程序健壮性

    1
    2
    3
    
    void f(const int i) {
        i++; // error!
    }
    
  • 节省空间,避免不必要的内存分配

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

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

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

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

1
2
// file1.cpp
int ext;

1
2
3
4
5
6
7
// file2.cpp
#include<iostream>

extern int ext;
int main() {
    std::cout << (ext + 10) << std::endl;
}

const 常量在不同文件的访问

1
2
// extern_file1.cpp
extern const int ext = 12; // 定义时要显示声明为 extern ,且要初始化

1
2
3
4
5
6
// extern_file2.cpp
#include<iostream>
extern const int ext;
int main() {
    std::cout << ext << std::endl;
}

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


4. 定义常量

1
2
3
4
const int b = 10;
b = 0; // error: assignment of read-only variable ‘b’
const std::string s = "helloworld";
const int i, j = 0; // error: uninitialized const ‘i’

上述有两个错误:

  • b 为常量,不可更改
  • i 为常量,必须进行初始化。(因为常量在定义后就不能被修改,所以定义时必须初始化)

5. 指针与 const

与指针相关的 const 有四种:

1
2
3
4
const char *a;        // 指向 const 对象的指针
char const *a;        // 同上
char *const a;        // 指向类型对象的 const 指针
const char *const a;  // 指向 const 对象的 const 指针

小结:
如果 const 位于 * 的左侧,则 const 是用来修饰指针所指向的变量,即指针指向为常量;
如果 const 位于 * 的右侧,const 就是修饰指针本身,即指针本身是常量

另一种解读方式
利用英文从右边往左边读,并以 to 为分界,to 之前为描述指针的特性,to 之后为描述目标的特性

1
2
3
4
const char *p;         // p is a pointer to const char
char const *p;         // 同上
char *const p;         // p is a const pointer to char
const char *const p;  // p is a const pointer to const char

当指针被加上 const 特性,则指针不可改变指向的地址;
当指向的目标特性为 char,则内容可以通过指针被修改,如: *char = 'y';
当指向的目标特性为 const char,则内容不可通过指针修改。

(1) 指向常量的指针

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

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

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

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

另外一个重点是:允许把非 const 对象的地址赋给指向 const 对象的指针

将非 const 对象的地址赋给 const 对象的指针:

1
2
3
4
const int *ptr;
int val = 3;
ptr = &val; // ok
*ptr = 1; // error

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

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

1
2
3
4
int *ptr1 = &val;
int val = 3;
*ptr1 = 4;
cout << *ptr << endl;

小结:

  1. 对于指向常量的指针,不能通过指针来修改对象的值
  2. 不能使用 void* 指针保存 const 对象的地址,必须使用 const void* 类型的指针保存const对象的地址
  3. 允许把非const对象的地址赋值给const对象的指针,如果要修改指针所指向的对象值,必须通过其他方式修改,不能直接通过当前指针直接修改

(2) 常指针

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

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main()
{
    int num = 0, num1 = 1;
    int *const ptr = &num; // const指针必须初始化 且const指针的指向不能修改
    ptr = &num1;           // error const指针不能修改指向
    cout << *ptr << endl;
}

代码出现编译错误:const指针不能修改指向

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main()
{
    int num = 0, num1 = 1;
    int *const ptr = &num; // const指针必须初始化 且const指针的指向不能修改
    *ptr = 1; // ok 修改指向的值
    cout << *ptr << endl;
}

代码无事发生,正常输出1

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

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int main()
{
    const int num = 0;
    int *const ptr = &num; // error! const int* -> int*
    cout << *ptr << endl;
}

上述若改为 const int *ptr或者改为const int *const ptr都可以:

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main()
{
    const int num = 10;
    const int *const ptr = &num;
    // const int *ptr = &num;
    cout << *ptr << endl;
}

小结:

  1. const 指针必须初始化,且指向的值能修改,指向不能修改
  2. const 指针能指向非 const 对象,但是 const 对象必须用 const 指针

(3)指向常量的常指针

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

1
2
const int p = 3;
const int * const ptr = &p; 

ptr是一个const指针,然后指向了一个int 类型的const对象


6.函数中使用const

const修饰函数返回值

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

(1)const int

1
const int func1();

这个本身无意义,因为参数返回本身就是赋值给其他的变量。

(2)const int*

1
const int* func2();

指针指向的内容不变。

(3)int *const

1
int *const func3();

指针本身不可变。

const修饰函数参数

(1)传递过来的参数及指针本身在函数内不可变,无意义

1
2
void func(const int var); // 传递过来的参数不可变
void func(int *const var); // 指针本身不可变

表明参数在函数体内不能被修改,但此处没有太多意义,var本身就是形参,加const只是保证在函数内不会改变。包括传入的形参是指针也是一样,加不加 const 对函数外效果都一样。

输入参数采用“值传递”,由于函数将自动产生临时变量用于复制该参数,该输入参数本来就无需保护,所以不要加const 修饰。

(2)参数指针所指内容为常量不可变

1
void StringCopy(char *dst, const char *src);

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

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

void change(int *dst, const int *src)
{
    *dst = 2;
    // *src = 2; // error
}

int main()
{
    int a = 1;
    int b = 1;
    int *ptr1 = &a;
    int *ptr2 = &b;
    change(ptr1, ptr2);
    cout << "ptr1 -> " << *ptr1 << endl;
    cout << "ptr2 -> " << *ptr2 << endl;
}

(3)参数为引用,为了增加效率同时防止修改。

1
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。

7.类中使用const

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

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

初始化

对于类中的const成员变量必须通过初始化列表进行初始化,如下所示:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Apple{
private:
    int people[100];
public:
    Apple(int i); 
    const int apple_number;
};

Apple::Apple(int i):apple_number(i)
{

}

访问

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

例如:

 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
33
34
35
36
37
38
#include <iostream>
using namespace std;

class Apple
{
private:
    int num;
public:
    Apple(int i); 
    const int apple_number;
    int add(int num);
    int take() const;
};

Apple::Apple(int i) : apple_number(i) {}

int Apple::add(int num)
{
    add(num);
    return 0;
}
int Apple::take() const
{
    add(1); // error
    return this->num;
}

int main()
{
    Apple a(2);
    a.add(10);
    a.take();
    cout << a.take() << endl;
    const Apple b(3);
    b.add(100); // error
    b.take();
    return 0;
}

代码有两个错误:

  1. const成员函数只能访问const成员函数
    此时报错,上面 take() 方法中调用了一个add()方法,而add()方法并非const修饰,所以运行报错
  2. const 对象只能访问 const 成员函数
    对象 a 能访问 add() 和 take()。而对象 b 用const修饰,无法访问add()方法,只能访问take()

其他初始化方法

我们除了上述的初始化const常量用初始化列表方式外,也可以通过下面方法:

第一:将常量定义与static结合:

1
static const int apple_number

第二:在外面初始化:

1
const int Apple::apple_number = 10;

当然,如果你使用c++11进行编译,直接可以在定义出初始化,可以直接写成:

1
2
3
static const int apple_number = 10;
// 或者
const int apple_number = 10;

这两种都在c++11中支持

编译的时候加上-std=c++11即可

这里提到了static,下面简单的说一下:

在C++中,非const的static静态成员变量不能在类的内部初始化。在类的内部只是声明,定义必须在类定义体的外部,通常在类的实现文件中初始化。

在类中声明:

1
static int ap;

在类实现文件中使用:

1
int Apple :: ap = 666

对于此项,c++11不能进行声明并初始化,也就是上述使用方法。

练习

常成员函数

 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
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;
class R
{
  public:
    R(int r1, int r2)
    {
        R1 = r1;
        R2 = r2;
    }
    // const区分成员重载函数
    void print();
    void print() const;

  private:
    int R1, R2;
};
/*
常成员函数说明格式:类型说明符  函数名(参数表)const;
这里,const是函数类型的一个组成部分,因此在实现部分也要带const关键字。
const关键字可以被用于参与对重载函数的区分
通过常对象只能调用它的常成员函数
*/

void R::print()
{
    cout << "普通调用" << endl;
    cout << R1 << ":" << R2 << endl;
}
// 实例化也需要带上
void R::print() const
{
    cout << "常对象调用" << endl;
    cout << R1 << ";" << R2 << endl;
}
int main()
{
    R a(5, 4);
    a.print(); // 调用void print()
    // 通过常对象只能调用它的常成员函数
    const R b(20, 52);
    b.print(); // 调用void print() const

    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
25
26
27
28
29
30

#include <iostream>
using namespace std;
void display(const double &r);

class A
{
  public:
    A(int i, int j)
    {
        x = i;
        y = j;
    }

  private:
    int x, y;
};
int main()
{
    double d(9.5);
    display(d);
    A const a(3, 4); // a 是常对象,不能被更新

    return 0;
}
void display(const double &r)
// 常引用做形参,在函数中不能更新 r 所引用的对象。
{
    cout << r << endl;
}
使用 Hugo 构建
主题 StackJimmy 设计