Chinaunix首页 | 论坛 | 博客
  • 博客访问: 97533
  • 博文数量: 21
  • 博客积分: 145
  • 博客等级: 入伍新兵
  • 技术积分: 250
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-22 17:37
文章分类

全部博文(21)

文章存档

2013年(16)

2012年(5)

我的朋友

分类: C/C++

2013-04-08 23:14:11

尝试总结一下const关键词在C++中的用法,不一定全面和正确,希望大家拍砖,共同进步。
参考:

1、替换#define
用const可以在编译阶段进行type检查,避免不必要的错误

点击(此处)折叠或打开

  1. const int size = 100; //编译器可能会把size放在symbol table里面,而不分配变量内存
  2. int array[size];


2、作为一个只初始化一次,
不可变的内存块

点击(此处)折叠或打开

  1. const int array[] = {10, 20, 30}
  2. int set[array[1]] //《Think in C++》里说会报错,但是貌似G++没有报错。

3、关于const指针


点击(此处)折叠或打开

  1. const int* a
  2. int const* a // 这两个是等价的,即指针指向的int不能改

  3. int * const a // 指针的指向是不可变的,但是所指的内容是可变的。

  4. // 一般用从右向左原则

4、关于传参
如果不想让函数调用影响传入的参数,可以有一下三种方式

点击(此处)折叠或打开

  1. void foo(const string &str)
  2. void foo(const string *str)
  3. void foo(string str) //这要copy,效率低

5、const成员函数

点击(此处)折叠或打开

  1. #include <iostream>

  2. using namespace std;

  3. class A {
  4.     public:
  5.         A() {} //默认构造函数要显式提供,不然 const A ca;无法初始化。

  6.         void f() {
  7.             cout << "f()" << endl;
  8.         }

  9.         void f() const {
  10.             cout << "f() const " << endl;
  11.         }
  12. };

  13. int main() {
  14.     A a;
  15.     a.f();
  16.     const A ca;
  17.     ca.f();

  18.     return 0;
  19. }

  20. 运行结果:
  21. f()
  22. f() const
从这个例子看出const是作为函数f()的function signature的;同时在函数重载时候,const对象调用const成员函数,non-const对象调用非const成员函数。

6、利用const形参进行函数重载

点击(此处)折叠或打开

  1. void foo(int i)
  2. void foo(const int i)
上面的两个函数重载是无法完成的,编译器会报 redefine错误。其实仔细分析一下是有道理的,例如代码foo(3);编译器根本无法选择一个函数来执行。

点击(此处)折叠或打开

  1. void foo(const int &i)
  2. void foo(int &i)
而这样是可以的,因为引用会影响外部调用的。

7、const修饰函数返回类型
一般要在有返回引用或者指针时候用到。

点击(此处)折叠或打开

  1. #include <iostream>

  2. using namespace std;

  3. class MyArray {
  4.     public:
  5.         MyArray() {}
  6.         int& operator[](int i) {
  7.             return array[i];
  8.         }

  9.     private:
  10.         int array[100];
  11. };

  12. void print(const MyArray &array) {
  13.     cout << array[1] << endl;
  14. }

  15. int main() {
  16.     MyArray myArray;
  17.     myArray[1] = 100;
  18.     cout << myArray[1] << endl;
  19.     print(myArray); // 报错

  20.     return 0;
  21. }
这里在print函数中,array是const对象,但是array[1]返回了引用,这是无法保证array是不变的,所以要加一个返回const引用 的const成员函数。

点击(此处)折叠或打开

  1. #include <iostream>

  2. using namespace std;

  3. class MyArray {
  4.     public:
  5.         //MyArray() {}
  6.         int& operator[](int i) {
  7.             return array[i];
  8.         }

  9.         const int& operator[](int i) const{
  10.             return array[i];
  11.         }

  12.     private:
  13.         int array[100];
  14. };

  15. void print(const MyArray &array) {
  16.     cout << array[1] << endl;
  17. }

  18. int main() {
  19.     MyArray myArray;
  20.     myArray[1] = 100;
  21.     cout << myArray[1] << endl;
  22.     print(myArray);

  23.     return 0;
  24. }





阅读(390) | 评论(0) | 转发(0) |
0

上一篇:POJ1159解题报告

下一篇:LeetCode 解题报告

给主人留下些什么吧!~~