Cpp--const限定符
2015-11-12 21:09
375 查看
1.定义const对象
const把一个对象转换成一个常量,因为常量在定义后不能修改,所以定义时必须初始化。[cpp] view
plaincopy
const std::string hi = "hello!"; // ok: initialized
const int i, j = 0; // error: i is uninitialized const
在全局作用域里定义非 const 变量时,它在整个程序中都可以访问。我们可以把一个非 const 变更定义在一个文件中,假设已经做了合适的声明,就可在另外的文件中使用这个变量:
[cpp] view
plaincopy
// file_1.cc
int counter; // definition
// file_2.cc
extern int counter; // uses counter from file_1
++counter; // increments counter defined in file_1
与其他变量不同,除非特别说明,在全局作用域声明的 const 变量是定义该对象的文件的局部变量。此变量只存在于那个文件中,不能被其他文件访问。
通过指定 const 变更为 extern,就可以在整个程序中访问 const 对象:
[cpp] view
plaincopy
// file_1.cc
// defines and initializes a const that is accessible to other files
extern const int bufSize = fcn();
// file_2.cc
extern const int bufSize; // uses bufSize from file_1
// uses bufSize defined in file_1
for (int index = 0; index != bufSize; ++index)
// ...
本程序中,file_1.cc 通过函数 fcn 的返回值来定义和初始化 bufSize。而 bufSize 定义为 extern,也就意味着 bufSize 可以在其他的文件中使用。file_2.cc 中 extern 的声明同样是 extern;这种情况下,extern 标志着bufSize 是一个声明,所以没有初始化式。
非 const 变量默认为 extern。要使 const 变量能够在其他的文件中访问,必须地指定它为 extern。
2.const引用
const 引用是指向 const 对象的引用:[cpp] view
plaincopy
const int ival = 1024;
const int &refVal = ival; // ok: both reference and object are
const int &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引用是指向非const类型变量的引用
[cpp] view
plaincopy
double dval = 23;
const int &ri = dval;
[cpp] view
plaincopy
编译器将其转换为:
[cpp] view
plaincopy
<span style="font-family: Arial, Helvetica, sans-serif;">int tmp = dval; // double -> int</span>
[cpp] view
plaincopy
<span style="font-family: Arial, Helvetica, sans-serif;">const int &ri = tmp;</span>
这是因为:
非 const 引用只能绑定到与该引用同类型的对象。
const 引用则可以绑定到不同但相关的类型的对象或绑定到右值
不允许非const引用指向需要临时对象的对象或值,即,编译器产生临时变量的时候引用必须为const
int iv = 100;
[cpp] view
plaincopy
int *&pir = &iv;//错误,非const引用对需要临时对象的引用
int *const &pir = &iv;//ok
const int ival = 1024;
int *&pi_ref = &ival; //错误,非const引用是非法的
const int *&pi_ref = &ival; //错误,需要临时变量,且引用的是指针,而pi_ref是一个非常量指针
const int * const &pi_ref = &ival; //正确
//补充
const int *p = &ival;
const int *&pi_ref = p; //正确
对于const int *const & pi_ref = &iva; 具体的分析如下:
1.不允许非const引用指向需要临时对象的对象或值
[cpp] view
plaincopy
int a = 2;
int &ref1 = a;// OK.有过渡变量。
const int &ref2 = 2;// OK.编译器产生临时变量,需要const引用
2.地址值是不可寻址的值
[cpp] view
plaincopy
int * const &ref3 = &a; // OK;
3.于是,用const对象的地址来初始化一个指向指针的引用
[cpp] view
plaincopy
const int b = 23;
const int *p = &b;
const int *& ref4 = p;
const int *const & ref5 = &b; //OK
3.指针与const限定符的关系
我们使用指针来修改其所指对象的值。但是如果指针指向const 对象,则不允许用指针来改变其所指的 const 值。为了保证这个特性,C++ 语言强制要求指向 const 对象的指针也必须具有 const 特性。[cpp] view
plaincopy
const double *cptr; // cptr may point to a double that is const
这里的 cptr 是一个指向 double 类型 const 对象的指针,const 限定了cptr 指针所指向的对象类型,而并非 cptr 本身。也就是说,cptr 本身并不是const。在定义时不需要对它进行初始化,如果需要的话,允许给 cptr 重新赋值,使其指向另一个 const 对象。但不能通过 cptr 修改其所指对象的值:
[cpp] view
plaincopy
*cptr = 42; // error: *cptr might be const
把一个 const 对象的地址赋给一个普通的、非 const 对象的指针也会导致编译时的错误:
[cpp] view
plaincopy
const double pi = 3.14;
double *ptr = π // error: ptr is a plain pointer
const double *cptr = π // ok: cptr is a pointer to const
不能使用 void* 指针保存 const 对象的地址,而必须使用 const void* 类型的指针保存 const 对象的地址:
[cpp] view
plaincopy
const int universe = 42;
const void *cpv = &universe; // ok: cpv is const
void *pv = &universe; // error: universe is const
允许把非 const 对象的地址赋给指向 const 对象的指针,例如:
[cpp] view
plaincopy
double dval = 3.14; // dval is a double; its value can be changed
cptr = &dval; // ok: but can't change dval through cptr
尽管 dval 不是 const 对象,但任何企图通过指针 cptr 修改其值的行为都会导致编译时的错误。cptr 一经定义,就不允许修改其所指对象的值。如果该指针恰好指向非 const 对象时,同样必须遵循这个规则。不能使用指向 const 对象的指针修改基础对象,然而如果该指针指向的是一个非 const 对象,可用其他方法修改其所指的对象。
事实是,可以修改 const 指针所指向的值,这一点常常容易引起误会。考虑:
[cpp] view
plaincopy
dval = 3.14159; // dval is not const
*cptr = 3.14159; // error: cptr is a pointer to const
double *ptr = &dval; // ok: ptr points at non-const double
*ptr = 2.72; // ok: ptr is plain pointer
cout << *cptr; // ok: prints 2.72
在此例题中,指向 const 的指针 cptr 实际上指向了一个非 const 对象。尽管它所指的对象并非 const,但仍不能使用 cptr 修改该对象的值。本质上来说,由于没有方法分辩 cptr 所指的对象是否为 const,系统会把它所指的所有对象都视为 const。如果指向 const 的指针所指的对象并非 const,则可直接给该对象赋值或间接地利用普通的非 const 指针修改其值:毕竟这个值不是 const。重要的是要记住:不能保证指向 const 的指针所指对象的值一定不可修改。如果把指向 const
的指针理解为“自以为指向 const 的指针”,这可能会对理解有所帮助。
在实际的程序中,指向 const 的指针常用作函数的形参。将形参定义为指向 const 的指针,以此确保传递给函数的实际对象在函数中不因为形参而被修改。
除指向const 对象的指针外,C++ 语言还提供了 const 指针——本身的值不能修改:
[cpp] view
plaincopy
int errNumb = 0;
int *const curErr = &errNumb; // curErr is a constant pointer
我们可以从右向左把上述定义语句读作“curErr 是指向 int 型对象的const 指针”。与其他 const 量一样,const 指针的值不能修改,这就意味着不能使 curErr 指向其他对象。任何企图给 const 指针赋值的行为(即使给curErr 赋回同样的值)都会导致编译时的错误:
[cpp] view
plaincopy
curErr = curErr; // error: curErr is const
与任何 const 量一样,const 指针也必须在定义时初始化。
指针本身是 const 的事实并没有说明是否能使用该指针修改它所指向对象的值。指针所指对象的值能否修改完全取决于该对象的类型。
例如,curErr 指向一个普通的非常量 int 型对象 errNumb,则可使用 curErr 修改该对象的值:
[cpp] view
plaincopy
if (*curErr) {
errorHandler();
*curErr = 0; // ok: reset value of the object to which curErr
is bound
}
还可以如下定义指向 const 对象的 const 指针:
[cpp] view
plaincopy
const double pi = 3.14159;// pi_ptr is const and points to a const object
const double *const pi_ptr = π
本例中,既不能修改 pi_ptr 所指向对象的值,也不允许修改该指针的指向(即 pi_ptr 中存放的地址值)。可从右向左阅读上述声明语句:“pi_ptr 首先是一个 const 指针,指向 double 类型的 const 对象”。
[cpp] view
plaincopy
string const s1; // s1 and s2 have same type,
const string s2; // they're both strings that are const
用 typedef 写 const 类型定义时,const 限定符加在类型名前面容易引起对所定义的真正类型的误解:
[cpp] view
plaincopy
string s;
typedef string *pstring;
const pstring cstr1 = &s; // written this way the type is obscured
pstring const cstr2 = &s; // all three decreations are the same type
string *const cstr3 = &s; // they're all const pointers to string
[cpp] view
plaincopy
把 const 放在类型 pstring 之后,然后从右向左阅读该声明语句就<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">会非常清楚地知道 cstr2 是 const pstring 类型,即指向 string 对</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">象的 const 指针。</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">不幸的是,大多数人在阅读 C++ 程序时都习惯看到 const 放在类型</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">前面。于是为了遵照惯例,只好建议编程时把 const 放在类型前面。</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">但是,把声明语句重写为置 const 于类型之后更便于理解。</span>
4.const限定符在函数形参中的运用:指针形参,引用形参,数组形参
const作为函数参数有六种可能。两种用于传值,四种用于传引用。函数传指针:指向非常量数据的非常量指针,指向非常量数据的常量指针,指向常量数据的非常量指针,指向常量数据的常量指针。
指向常量数据的非常量指针:接收一个数据参数,然后让函数在不修改数据的情况下处理数组的每一个元素。(实参是地址,形参定义为指针)
指向非常量数据的常量指针:作为接收一个数组的函数参数,函数只用数组下标就可以访问数组元素。
在调用函数时,如果该函数使用非引用的非 const 形参,则既可给该函数传递 const 实参也可传递非 const 的实参。
例如,可以传递两个 int 型 const对象调用 gcd:
[cpp] view
plaincopy
const int i = 3, j = 6;
int k = rgcd(3, 6); // ok: k initialized to 3
这种行为源于 const 对象的标准初始化规则(上面已经介绍)。因为初始化复制了初始化式的值,所以可用 const 对象初始化非 const 对象,反之亦然。
如果将形参定义为非引用的 const 类型:
[cpp] view
plaincopy
void fcn(const int i) { /* fcn can read but not write to i */ }
则在函数中,不可以改变实参的局部副本。由于实参仍然是以副本的形式传递,因此传递给 fcn 的既可以是 const 对象也可以是非 const 对象。令人吃惊的是,尽管函数的形参是 const,但是编译器却将 fcn 的定义视为其形码被声明为普通的 int 型:
[cpp] view
plaincopy
void fcn(const int i) { /* fcn can read but not write to i */ }
void fcn(int i) { /* ... */ } // error: redefines fcn(int)
这种用法是为了支持对 C 语言的兼容,因为在 C 语言中,具有 const 形参或非 const 形参的函数并无区别。
编写一个比较两个 string 对象长度的函数作为例子。这个函数需要访问每个 string 对象的 size,但不必修改这些对象。由于 string 对象可能相当长,所以我们希望避免复制操作。使用 const 引用就可避免复制:
[cpp] view
plaincopy
// compare the length of two strings
bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
其每一个形参都是 const string 类型的引用。因为形参是引用,所以不复制实参。又因为形参是 const 引用,所以 isShorter 函数不能使用该引用来修改实参。如果使用引用形参的唯一目的是避免复制实参,则应将形参定义为 const 引用。
如果函数具有普通的非 const 引用形参,则显然不能通过 const 对象进行调用。毕竟,此时函数可以修改传递进来的对象,这样就违背了实参的 const 特性。
但比较容易忽略的是,调用这样的函数时,传递一个右值或具有需要转换的类型的对象同样是不允许的:
[cpp] view
plaincopy
// function takes a non-const reference parameter
int incr(int &val)
{
return ++val;
}
int main()
{
short v1 = 0;
const int v2 = 42;
int v3 = incr(v1); // error: v1 is not an int
v3 = incr(v2); // error: v2 is const
v3 = incr(0); // error: literals are not lvalues
v3 = incr(v1 + v2); // error: addition doesn't yield an lvalue
int v4 = incr(v3); // ok: v3 is a non const object type int
}
问题的关键是非 const 引用形参只能与完全同类型的非 const 对象关联。应该将不修改相应实参的形参定义为 const 引用。如果将这样的形参定义为非 const 引用,则毫无必要地限制了该函数的使用。例如,可编写下面的程序在一个 string 对象中查找一个指定的字符:
[cpp] view
plaincopy
// 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 const
string::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 character
return i;
}
这个函数将其 string 类型的实参当作普通(非 const)的引用,尽管函数并没有修改这个形参的值。这样的定义带来的问题是不能通过字符串字面值来调用这个函数:
[cpp] view
plaincopy
if (find_char("Hello World", 'o')) // ...
虽然字符串字面值可以转换为 string 对象,但上述调用仍然会导致编译失败。继续将这个问题延伸下去会发现,即使程序本身没有 const 对象,而且只使用 string 对象(而并非字符串字面值或产生 string 对象的表达式)调用find_char 函数,编译阶段的问题依然会出现。例如,可能有另一个函数is_sentence 调用 find_char 来判断一个 string 对象是否是句子:
[cpp] view
plaincopy
bool is_sentence (const string &s)
{
// if there's a period and it's the last character in s
// then s is a sentence
return (find_char(s, '.') == s.size() - 1);
}
如上代码,函数 is_sentence 中 find_char 的调用是一个编译错误。传递进 is_sentence 的形参是指向 const string 对象的引用,不能将这种类型的参数传递给 find_char,因为后者期待得到一个指向非 const string 对象的引用。应该将不需要修改的引用形参定义为 const 引用。普通的非 const 引用形参在使用时不太灵活。这样的形参既不能用 const 对象初始化,也不能用字面值或产生右值的表达式实参初始化。
5.const类成员函数
现在,可以理解跟在 Sales_item 成员函数声明的形参表后面的 const 所起的作用了:const 改变了隐含的 this 形参的类型。在调用total.same_isbn(trans) 时,隐含的 this 形参将是一个指向 total 对象的const Sales_Item* 类型的指针。就像如下编写 same_isbn 的函数体一样:[cpp] view
plaincopy
// pseudo-code illustration of how the implicit this pointer is used
// This code is illegal: We may not explicitly define the this pointer ourselves
// Note that this is a pointer to const because same_isbn is a const member
bool Sales_item::same_isbn(const Sales_item *const this,
const Sales_item &rhs) const
{ return (this->isbn == rhs.isbn); }
用这种方式使用 const 的函数称为 常量成员函数。由于 this 是指向 const 对象的指针,const 成员函数不能修改调用该函数的对象。因此,函数 avg_price和函数 same_isbn 只能读取而不能修改调用它们的对象的数据成员。const 对象、指向 const 对象的指针或引用只能用于调用其const 成员函数,如果尝试用它们来调用非 const 成员函数,则是错误的。
6.const对象的动态数组
如果我们在自由存储区中创建的数组存储了内置类型的 const 对象,则必须为这个数组提供初始化:因为数组元素都是 const 对象,无法赋值。实现这个要求的唯一方法是对数组做值初始化:// error: uninitialized const array const int *pci_bad = new const int[100]; // ok: value-initialized const array const int *pci_ok = new const int[100]();C++ 允许定义类类型的 const 数组,但该类类型必须提供默认构造函数:
// ok: array of 100 empty strings const string *pcs = new const string[100];在这里,将使用 string 类的默认构造函数初始化数组元素。当然,已创建的常量元素不允许修改——因此这样的数组实际上用处不大。
在C++中,允许动态创建const对象,格式如下:
const int *p = new const int(128);与其他常量一样,动态创建的const对象必须在创建时初始化,并且初始化后,其值不能改变。
如一个完整的例子:
#include <iostream> using namespace std; int main() { int num; cin>>num; const int *pNum = new const int(num); int arr[*pNum]; for(int i=0;i<*pNum;++i) arr[i] = i; for(int i=0;i<*pNum;++i) cout<<arr[i]<<" "; cout<<endl; return 0; }
7.const在指针初始化和赋值操作中的约束
把 int 型变量赋给指针是非法的,尽管此 int 型变量的值可能为 0。但允许把数值 0 或在编译时可获得 0 值的 const 量赋给指针:int ival; int zero = 0; const int c_ival = 0; int *pi = ival; // error: pi initialized from int value of ival pi = zero; // error: pi assigned int value of zero pi = c_ival; // ok: c_ival is a const with compile-time value of 0 pi = 0; // ok: directly initialize to literal constant 0除了使用数值 0 或在编译时值为 0 的 const 量外,还可以使用 C++ 语言从 C 语言中继承下来的预处理器变量 NULL,该变量在 cstdlib头文件中定义,其值为 0。如果在代码中使用了这个预处理器变量,则编译时会自动被数值 0 替换。因此,把指针初始化为 NULL 等效于初始化为 0 值:
// cstdlib #defines NULL to 0 int *pi = NULL; // ok: equivalent to int *pi = 0;正如其他的预处理器变量一样,不可以使用 NULL 这个标识符给自定义的变量命名。
相关文章推荐
- 在Ubuntu上使用eclipse开发C++[持续更新...]
- C/C++ debug(一)
- __stdcall,__cdcel,extern c 和导出函数名
- 结合C++和GDAL实现shapefile(shp)文件的创建和写入
- C++中的delete和delete[]区别
- 网易MOOC教育之软件工程(C编码实践篇)学习总结
- leetcode笔记:Best Time to Buy and Sell Stock IV
- c++学习第一课
- 最大公共子串(C语言实现)
- C++中的vector< vector<int> > v(m, vector<int>(n) );是什么意思
- C语言格式化输入输出
- 关于C++中using namespace std
- C++中现成的hash函数
- C++primer第五版第十章学习笔记
- C++ DirectX 游戏开发视频教程 01 资源下载链接
- C语言(C++语言)中##(两个井号)和#(一个井号)用法
- C语言中更改控制台中文字颜色
- 结合C++和GDAL实现shapefile(shp)文件的读取
- C语言中的一些时间函数(time/sleep/clock)
- Win7 下 CDT 工具链的选择