const小结

来源:互联网 发布:js onclick 传递对象 编辑:程序博客网 时间:2024/05/14 03:17

1.const对象

const 限定符提供了一个解决办法,它把一个对象转换成一个常量。
因为常量在定义后就不能被修改,所以定义时必须初始化:
const std::string hi = "hello!"; // ok: initializedconst int i, j = 0; // error: i is uninitialized const
const 对象默认为文件的局部变量。
非 const 变量默认为 extern。要使 const 变量能够在其他的文件中访问,必须地指定它为 extern。
在全局作用域里定义非 const 变量时,它在整个程序中都可以访问。
我们可以把一个非 const 变更定义在一个文件中,假设已经做了合适的声明,就可在另外的文件中使用这个变量:
// file_1.ccint counter; // definition// file_2.ccextern int counter; // uses counter from file_1++counter; // increments counter defined in file_1
与其他变量不同,除非特别说明,在全局作用域声明的 const 变量是定义该对象的文件的局部变量。此变量只存在于那个文件中,不能被其他文件访问。
通过指定 const 变更为 extern,就可以在整个程序中访问 const 对象:
// file_1.cc// defines and initializes a const that is accessible to other filesextern const int bufSize = fcn();// file_2.ccextern const int bufSize; // uses bufSize from file_1// uses bufSize defined in file_1for (int index = 0; index != bufSize; ++index)// ...
本程序中,file_1.cc 通过函数 fcn 的返回值来定义和初始化 bufSize。而 bufSize 定义为 extern,也就意味着 bufSize 可以在其他的文件中使用。file_2.cc 中 extern 的声明同样是 extern;这种情况下,extern 标志着bufSize 是一个声明,所以没有初始化式。

2.const 引用

const 引用是指向 const 对象的引用:
const int ival = 1024;const int &refVal = ival; // ok: both reference and object are constint &ref2 = ival; // error: non const reference to a const object
可以读取但不能修改 refVal ,因此,任何对 refVal 的赋值都是不合法的。这个限制有其意义:不能直接对 ival 赋值,因此不能通过使用 refVal 来修改ival。
同理,用 ival 初始化 ref2 也是不合法的:ref2 是普通的非 const 引用,因此可以用来修改 ref2 指向的对象的值。通过 ref2 对 ival 赋值会导致修改const 对象的值。为阻止这样的修改,需要规定将普通的引用绑定到 const 对象是不合法的。
const 引用可以初始化为不同类型的对象或者初始化为右值,如字面值常量:
int i = 42;// legal for const references onlyconst int &r = 42;const int &r2 = r + i;
同样的初始化对于非 const 引用却是不合法的,而且会导致编译时错误。其原因非常微妙,值得解释一下。
观察将引用绑定到不同的类型时所发生的事情,最容易理解上述行为。假如我们编写
double dval = 3.14;const int &ri = dval;
编译器会把这些代码转换成如以下形式的编码:
int temp = dval; // create temporary int from the doubleconst int &ri = temp; // bind ri to that temporary
const引用绑定到不同类型的对象或右值,会先生成一个与引用类型相同的临时对象(必要时进行类型转换),然后引用变量再与该临时对象绑定。
如果 ri 不是 const,那么可以给 ri 赋一新值。这样做不会修改 dval,而是修改了 temp。期望对 ri 的赋值会修改 dval 的程序员会发现 dval 并没有被修改。仅允许 const 引用绑定到需要临时使用的值完全避免了这个问题,因为 const 引用是只读的。
非 const 引用只能绑定到与该引用同类型的对象。
const 引用则可以绑定到不同但相关的类型的对象或绑定到右值。


3.利用const 引用形参避免复制

使用引用形参,函数可以直接访问实参对象,而无须复制它。
// compare the length of two stringsbool isShorter(const string &s1, const string &s2){return s1.size() < s2.size();}
其每一个形参都是 const string 类型的引用。因为形参是引用,所以不复制实参。又因为形参是 const 引用,所以 isShorter 函数不能使用该引用来修改实参。
如果使用引用形参的唯一目的是避免复制实参,则应将形参定义为 const 引用。
应该将不修改相应实参的形参定义为 const 引用,这样更灵活。
如果将这样的形参定义为非 const 引用,则毫无必要地限制了该函数的使用。如果函数具有普通的非 const 引用形参,则显然不能通过 const 对象进行调用。例如,可编写下面的程
序在一个 string 对象中查找一个指定的字符:
// returns index of first occurrence of c in s or s.size() if c isn't in s// Note: s doesn't change, so it should be a reference to conststring::size_type find_char(string &s, char c){string::size_type i = 0;while (i != s.size() && s[i] != c)++i; // not found, look at next characterreturn i;}
这个函数将其 string 类型的实参当作普通(非 const)的引用,尽管函数并没有修改这个形参的值。这样的定义带来的问题是不能通过字符串字面值来调用这个函数:
if (find_char("Hello World", 'o')) // ...
虽然字符串字面值可以转换为 string 对象,但上述调用仍然会导致编译失败。

4.指向const 对象的指针

如果指针指向const 对象,则不允许用指针来改变其所指的 const 值。为了保证这个特性,C++ 语言强制要求指向 const 对象的指针也必须具有 const 特性:
const double *cptr; // cptr may point to a double that is const
这里的 cptr 是一个指向 double 类型 const 对象的指针,const 限定了cptr 指针所指向的对象类型,而并非 cptr 本身。也就是说,cptr 本身并不是const。在定义时不需要对它进行初始化,如果需要的话,允许给 cptr 重新赋值,使其指向另一个 const 对象。但不能通过 cptr 修改其所指对象的值:
*cptr = 42; // error: *cptr might be const
把一个 const 对象的地址赋给一个普通的、非 const 对象的指针也会导致编译时的错误:
const double pi = 3.14;double *ptr = π // error: ptr is a plain pointerconst double *cptr = π // ok: cptr is a pointer to const
不能使用 void* 指针(第 4.2.2 节)保存 const 对象的地址,而必须使用 const void* 类型的指针保存 const 对象的地址:
const int universe = 42;const void *cpv = &universe; // ok: cpv is constvoid *pv = &universe; // error: universe is const
允许把非 const 对象的地址赋给指向 const 对象的指针,例如:
double dval = 3.14; // dval is a double; its value can be changedcptr = &dval; // ok: but can't change dval through cptr
尽管 dval 不是 const 对象,但任何企图通过指针 cptr 修改其值的行为都会导致编译时的错误。cptr 一经定义,就不允许修改其所指对象的值。如果该指针恰好指向非 const 对象时,同样必须遵循这个规则。
如果指向 const 的指针所指的对象并非 const,则可直接给该对象赋值或间接地利用普通的非 const 指针修改其值:毕竟这个值不是 const。重要的是要记住:不能保证指向 const 的指针所指对象的值一定不可修改。
如果把指向 const 的指针理解为“自以为指向 const 的指针”,这可能会对理解有所帮助。
在实际的程序中,指向 const 的指针常用作函数的形参。将形参定义为指向 const 的指针,以此确保传递给函数的实际对象在函数中不因为形参而被修改。
例如:
void use_ptr(const int *p){// use_ptr may read but not write to *p}
保护*p不被修改。可以将指向 const 对象的指针初始化为指向非 const对象,但不可以让指向非 const 对象的指针向 const 对象。

5.const 指针

除指向 const 对象的指针外,C++ 语言还提供了 const 指针——本身的值不能修改(存放的地址不能修改):
int errNumb = 0;int *const curErr = &errNumb; // curErr is a constant pointer
我们可以从右向左把上述定义语句读作“curErr 是指向 int 型对象的const 指针”。与其他 const 量一样,const 指针的值不能修改,这就意味着不能使 curErr 指向其他对象。任何企图给 const 指针赋值的行为(即使给curErr 赋回同样的值)都会导致编译时的错误:
curErr = curErr; // error: curErr is const
与任何 const 量一样,const 指针也必须在定义时初始化。
指针本身是 const 的事实并没有说明是否能使用该指针修改它所指向对象的值。指针所指对象的值能否修改完全取决于该对象的类型。

6.指向const 对象的 const 指针

还可以如下定义指向 const 对象的 const 指针:
const double pi = 3.14159;// pi_ptr is const and points to a const objectconst double *const pi_ptr = π
本例中,既不能修改 pi_ptr 所指向对象的值,也不允许修改该指针的指向(即 pi_ptr 中存放的地址值)。可从右向左阅读上述声明语句:“pi_ptr 首先是一个 const 指针,指向 double 类型的 const 对象”。

7.const 对象的动态分配和回收

C++ 允许动态创建 const 对象:
// allocate and initialize a const objectconst int *pci = new const int(1024);
与其他常量一样,动态创建的 const 对象必须在创建时初始化,并且一经初始化,其值就不能再修改。上述 new 表达式返回指向 int 型 const 对象的指针。与其他 const 对象的地址一样,由于 new 返回的地址上存放的是 const对象,因此该地址只能赋给指向 const 的指针。对于类类型的 const 动态对象,如果该类提供了默认的构造函数,则此对
象可隐式初始化:
// allocate default initialized const empty stringconst string *pcs = new const string;
new 表达式没有显式初始化 pcs 所指向的对象,而是隐式地将 pcs 所指向的对象初始化为空的 string 对象。内置类型对象或未提供默认构造函数的类类型对象必须显式初始化。
删除const 对象
尽管程序员不能改变 const 对象的值,但可撤销对象本身。如同其他动态对象一样, const 动态对象也是使用删除指针来释放的:
delete pci; // ok: deletes a const object

即使 delete 表达式的操作数是指向 int 型 const 对象的指针,该语句同样有效地回收 pci 所指向的内容。


8.const 对象的动态数组

如果我们在自由存储区中创建的数组存储了内置类型的 const 对象,则必须为这个数组提供初始化:因为数组元素都是 const 对象,无法赋值。实现这
个要求的唯一方法是对数组做值初始化:
// error: uninitialized const arrayconst int *pci_bad = new const int[100];// ok: value-initialized const arrayconst int *pci_ok = new const int[100]();
C++ 允许定义类类型的 const 数组,但该类类型必须提供默认构造函数:
// ok: array of 100 empty stringsconst string *pcs = new const string[100];
在这里,将使用 string 类的默认构造函数初始化数组元素。
当然,已创建的常量元素不允许修改——因此这样的数组实际上用处不大。


9.const 成员函数

成员函数声明的形参表后面的 const 所起的作用了:const 改变了隐含的 this 形参的类型。例如:
bool same_isbn(const Sales_item &rhs) const{ return isbn == rhs.isbn; }
在调用total.same_isbn(trans) 时,隐含的 this 形参将是一个指向 total 对象的const Sales_Item* 类型的指针。由于 this 是指向 const 对象的指针,const 成员函数不能修改调用该函数的对象。因此,和函数 same_isbn 只能读取而不能修改调用它们的对象的数据成员。
const 对象、指向 const 对象的指针或引用只能用于调用其const 成员函数,如果尝试用它们来调用非 const 成员函数,则是错误的。

10.const返回值

修饰返回值的const,如const A fun2( ); const A* fun3( ); 

这样声明了返回值后,const按照"修饰原则"进行修饰,起到相应的保护作用。

const Rational operator*(const Rational& lhs, const Rational& rhs) { return Rational(lhs.numerator() * rhs.numerator(), lhs.denominator() * rhs.denominator()); } 
返回值用const修饰可以防止允许这样的操作发生:
Rational a,b; Radional c; (a*b) = c; //错误,const返回值禁止作为左值调用
一般用const修饰返回值为对象本身(非引用和指针)的情况多用于二目操作符重载函数并产生新对象的时候。




参考:《C++Primer》4th
http://cxsj.neu.edu.cn/cxsj/online/C2/C2_9.html
http://www.cnblogs.com/BloodAndBone/archive/2011/04/12/2013280.html
http://hi.baidu.com/xaequjslqkbbfmd/item/67c9618b298629804414cf53
http://blog.csdn.net/qin3232521/article/details/7687210
原创粉丝点击