在课程 5.1 -- 常量变量(命名常量), 你学到,基本数据类型的对象 (int, double, char, etc…) 可以通过 const 关键字声明为常量。所有 const 变量都必须在创建时初始化。
const int x; // 编译错误:未初始化
const int y{}; // 正确:值初始化
const int z{ 5 }; // 正确:列表初始化
同样,类类型对象(结构体、类和联合体)也可以使用 const 关键字声明为 const。这些对象也必须在创建时初始化。
struct Date
{
int year {};
int month {};
int day {};
};
int main()
{
const Date today { 2020, 10, 14 }; // const 类类型对象
return 0;
}
与普通变量一样,如果需要确保类类型对象创建后不被修改,通常应将其设为 const(或 constexpr)。
不允许修改 const 对象的数据成员
const 类对象初始化后,任何修改其数据成员的尝试都会被禁止,因为这会违反对象的 const 属性。这包括直接修改成员变量(如果它们是 public),或调用会设置成员变量值的成员函数。
struct Date
{
int year {};
int month {};
int day {};
void incrementDay()
{
++day;
}
};
int main()
{
const Date today { 2020, 10, 14 }; // const
today.day += 1; // 编译错误:不能修改 const 对象的成员
today.incrementDay(); // 编译错误:不能调用会修改 const 对象成员的成员函数
return 0;
}
const 对象不能调用非 const 成员函数
下面的代码同样会导致编译错误:
#include <iostream>
struct Date
{
int year {};
int month {};
int day {};
void print()
{
std::cout << year << '/' << month << '/' << day;
}
};
int main()
{
const Date today { 2020, 10, 14 }; // const
today.print(); // 编译错误:不能调用非 const 成员函数
return 0;
}
即使 print() 并没有尝试修改成员变量,因此对 today.print() 仍然违反 const 限制。这是因为 print() 成员函数本身没有声明为 const。编译器不允许在 const 对象上调用非 const 成员函数。
const 成员函数
为解决上述问题,需要将 print() 声明为 const 成员函数。 const 成员函数 是一种保证不会修改对象,也不会调用任何非 const 成员函数(因为它们可能修改对象)的成员函数。
将 print() 将成员函数设为 const 很简单——只需在 const 关键字追加到函数原型中,放在参数列表之后、函数体之前:
#include <iostream>
struct Date
{
int year {};
int month {};
int day {};
void print() const // 现在是 const 成员函数
{
std::cout << year << '/' << month << '/' << day;
}
};
int main()
{
const Date today { 2020, 10, 14 }; // const
today.print(); // 正确:const 对象可以调用 const 成员函数
return 0;
}
在上面的例子中, print() 已被设为 const 成员函数,因此可以在 const 对象(例如 today).
进阶读者
对于在类定义外定义的成员函数, const 关键字必须同时出现在类内的函数声明和类外的函数定义中。课程 15.2 -- 类与头文件。
构造函数不能设为 const,因为它们需要初始化对象成员,也就需要修改成员。构造函数见课程 14.9 -- 构造函数简介。
const 成员函数如果尝试修改数据成员或调用非 const 成员函数,会导致编译错误。例如:
struct Date
{
int year {};
int month {};
int day {};
void incrementDay() const // 设为 const
{
++day; // 编译错误:const 成员函数不能修改成员
}
};
int main()
{
const Date today { 2020, 10, 14 }; // const
today.incrementDay();
return 0;
}
在这个示例中, incrementDay() 已标记为 const 成员函数,但它试图修改 day。这会导致编译错误。
const 成员函数可以像往常一样修改非成员对象(如局部变量和函数参数),也可以调用非成员函数。const 限制只适用于成员。
关键要点
const 成员函数不能:修改隐式对象,调用非 const 成员函数。
const 成员函数可以:修改不是隐式对象的其他对象,调用 const 成员函数,调用非成员函数。
const 成员函数可以在非 const 对象上调用
const 成员函数也可以在非 const 对象上调用:
#include <iostream>
struct Date
{
int year {};
int month {};
int day {};
void print() const // const
{
std::cout << year << '/' << month << '/' << day;
}
};
int main()
{
Date today { 2020, 10, 14 }; // 非 const
today.print(); // 正确:非 const 对象可以调用 const 成员函数
return 0;
}
由于 const 成员函数可以在 const 和非 const 对象上调用,如果成员函数不会修改对象状态,就应将其设为 const。
最佳实践
不会(也永远不会)修改对象状态的成员函数应设为 const,这样就能在 const 和非 const 对象上调用。
谨慎决定为哪些成员函数添加 const 。成员函数设为 const 后,就可以在 const 对象上调用。之后如果删除 const ,所有在 const 对象上调用该成员函数的代码都会失效。
通过 const 引用传递的 const 对象
虽然实例化 const 局部变量可以创建 const 对象,但更常见的方式是通过 const 引用将对象传给函数。
在课程 12.5 -- 按左值引用传递中,我们介绍了类类型实参按 const 引用而不是按值传递的优点。总结一下,按值传递类类型实参会复制整个类(速度较慢);大多数时候不需要副本,引用原始实参即可,并且可以避免复制。通常将引用设为 const,以便函数接收 const 左值实参和右值实参(例如字面量和临时对象)。
你能看出下面代码有什么问题吗?
#include <iostream>
struct Date
{
int year {};
int month {};
int day {};
void print() // 非 const
{
std::cout << year << '/' << month << '/' << day;
}
};
void doSomething(const Date& date)
{
date.print();
}
int main()
{
Date today { 2020, 10, 14 }; // 非 const
today.print();
doSomething(today);
return 0;
}
答案是,在 doSomething() 函数, date 被视为 const 对象(因为它是按 const 引用传递的)。由于这个 const date,我们调用了非 const 成员函数 print()。由于不能在 const 对象上调用非 const 成员函数,这会导致编译错误。
修复很简单:将 print() const:
#include <iostream>
struct Date
{
int year {};
int month {};
int day {};
void print() const // 现在为 const
{
std::cout << year << '/' << month << '/' << day;
}
};
void doSomething(const Date& date)
{
date.print();
}
int main()
{
Date today { 2020, 10, 14 }; // 非 const
today.print();
doSomething(today);
return 0;
}
现在,在函数 doSomething() 中,const date 可以成功调用 const 成员函数 print()。
成员函数的 const 与非 const 重载
最后,虽然并不常见,也可以为成员函数重载 const 和非 const 两个版本。这是因为 const 限定符属于函数签名的一部分,因此只有 const 性质不同的两个函数也会被视为不同函数。
#include <iostream>
struct Something
{
void print()
{
std::cout << "non-const\n";
}
void print() const
{
std::cout << "const\n";
}
};
int main()
{
Something s1{};
s1.print(); // 调用 print()
const Something s2{};
s2.print(); // 调用 print() const
return 0;
}
输出结果:
non-const
const
通常只有在返回值的 const 性质需要不同时,才会重载 const 和非 const 版本。这种情况很少见。