三、尽可能使用const
再细心的程序员也会有犯错的时候。通过语言自身的机制来对程序产生约束,可以大大减少错误的发生。而如何利用这些机制就要看程序员的习惯了。
事实上,这些机制其实就是让编译器能更加准确地理解程序员的用意, 这样当程序运行方式与程序员的真实用意不相符时,就可以提醒程序员“这个地方会不按要求执行,请修改一下”。
const就是一个很好的例子。它让编译器知道程序员定义的变量是不允许被更改的,编译器会强制实施这个约束。
我在前一篇文章里对const作了一些介绍。const在STL中也有用到,比如说迭代器,如果不想让迭代器指向不同的东本,可以定义它为const_iterator。
const还可以与函数返回值,参数和成员函数自身产生关联。
const与返回值关联和参数
将函数参与设定为const比较好理解,就是不希望在函数内部出现更改参数的行为。但为什么有时候还要将返回值也设定为const呢?我们来看下面的例子:
#include <iostream>
using namespace std;
class Complex {
public:
double real, imag;
Complex(double r, double i): real(r), imag(i){}
Complex operator+(const Complex &);
bool operator==(const Complex &) const;
operator void*();
};
Complex Complex::operator +(const Complex &c) {
return Complex(this->real + c.real, this->imag + c.imag);
}
bool Complex::operator==(const Complex &c) const {
return this->real == c.real && this->imag == c.imag;
}
Complex::operator void *() {
return this;
}
int main() {
Complex a(1, 2), b(2, 3), c(3, 4);
if(a + b = c)
cout << "a plus b equals to c" << endl;
else
cout << "a plus b does not equal to c" << endl;
}
在main函数中,本来是要判断虚数a + b与c是否相等,但由于少写了一个等号,所以程序运行结果成了:
a plus b equals to c
这显然是不对的。
这时我们就可以看出将返回值设为const的用处了,如果将+运算定义为:
const Complex operator+(const Complex &);
那么当要将c赋值给a+b的结果时,编译器会报错,这样我们就能很快发现问题了。
事实上,C++的内置类型就是这么做的,比如说如果a,b,c都是int型,a+b=c也会编译报错。将返回值设为const可以预防“没意思的赋值动作“
const 成员函数
类似于Java的final,C++中const对象只能调用const成员函数。上例中的==重载就是const成员函数。在const成员函数中不允许修改对象的属性。
当我们用STL的priority_queue时,需要重载<运算符,而且必须声明为const 成员函数。这是因为,priority_queue的定义为:
template <class T, class Container = vector<T>,
class Compare = less<typename Container::value_type> > class priority_queue;
其中用到了默认为less的比较类来比较大小。less定义如下:
template <class T> struct less {
bool operator() (const T& x, const T& y) const {return x<y;}
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};
这是一个函数对象,其成员函数是const的,所以不允许x和y的值被改动。这就要求<运算也是const的。这也是一个很好的习惯。我们不希望在比较两个对象的过程改变对象的值。
两个成员函数如果只是常量性不同,可以被重载。
#include <iostream>
using namespace std;
class A {
public:
void print() {
cout << "this is print" << endl;
}
void print() const {
cout << "this is const print" << endl;
}
};
void test1(A a) {
a.print();
}
void test2(const A a) {
a.print();
}
int main() {
A a;
test1(a);
test2(a);
return 0;
}
输出结果为:
this is print
this is const print
当对象为const时,由于常量要求只能调用const成员函数,所以test2调用的是第二个print函数。而a不是常量,test1将调用第一个print().
C++如何保证常量性
C++使用的是bitwise constness(又称physical constness)来保证常量性的。也就是说const成员函数不能修改对象的任何一个bit,即不存在对成员属性的赋值操作(static成员变量除外)。
这样看似非常保险了,但其实bitwise constness有些时候达不到我们对const的要求。比如下面的例子:
#include <iostream>
using namespace std;
class MyString {
char *s;
public:
MyString(string a) {
int len = a.length();
s = new char[len + 1];
for(int i=0; i<len; i++)
s[i] = a[i];
s[len] = '\0';
}
char &operator[](int position) const {
return s[position];
}
void print() const {
cout << s << endl;
}
};
int main() {
const MyString test("hello");
test[0] = 'H';
test.print();
return 0;
}
因为test对象里只有一个指针,而[]运算符里也没有更改这个指针,所以符合bitwise constness。但结果是反直观的,因为我们将test声明为const是想让test内容不被改变。
这就引出了logical constness。这指的是一个const成员函数可以修改对象内的某些bits,但只有在客户端侦测不到的情况下才能如此(比如类似缓存结果),以下是书中的一个例子:
#include <iostream>
#include <cstring>
using namespace std;
class CTextBlock {
char *pText;
mutable size_t textLength;
mutable bool lengthIsValid;
public:
size_t length() const;
};
size_t CTextBlock::length() const {
if(!lengthIsValid) {
lengthIsValid = true;
textLength = strlen(pText);
}
return textLength;
}
mutable关键字可以解决bitwise constness的约束。上例实现了一个logical constness。
有时候可能希望一个const对象临时调用一个非const的方法,这时可以利用const_cast将const对象转成非const的,然后再利用static_cast将其转回到const。