c/c++中const详解
来自http://blog.csdn.net/lwbeyond/article/details/6187501
一. cons 的作用
(1) 可以定义 const 常量
- const int Max=100;
-
- int Array[Max];
(2) 可以保护被修饰的东西,防止意外的修改,增强程序的健壮性
如果在函数体内修改了i,编译器就会报错;
- void f (const int i)
- {
- i=10;
- }
(3) 为函数重载提供了一个参考
- class A
- {
- ......
- void f(int i) {......}
- void f(int i) const {......}
- ......
- };
(4) 可以节省空间,避免不必要的内存分配
- #define PI 3.14159 //常量宏
- const doulbe Pi=3.14159;
- ......
- double i=Pi;
- double I=PI;
- double j=Pi;
- double J=PI;
const定义常量从汇编的角度来看,只是给出了对应的内存地址,而不是象#define一样给出的是立即数,所以,const定义的常量在程序运行过程中只有一份拷贝,而#define定义的常量在内存中有若干个拷贝。
(5) 提高了效率
编译器通常不为普通const常量分配存储空间,而是将它们保存在符号表中,这使得它成为一个编译期间的常量,没有了存储与读内存的操作,使得它的效率也很高。
二. 使用 const
(1) 修饰一般常量,常数组,常对象
修饰符const可以用在类型说明符前,也可以用在类型说明符后。 例如:
- int const x=2;
- const int x=2;
-
- int const a[5]={1, 2, 3, 4, 5};
- const int a[5]={1, 2, 3, 4, 5};
-
- class A;
- const A a;
- A const a;
(2) 修饰指针
- const int *A;
- int const *A;
- int *const A;
- const int *const A;
<<effective C++>>上有个好记的方法:const在*号左边修饰的是指针所指的内容;const在*号右边修饰的是指针。
简单记就是:左内容,右指针。
也可以这样理解:先忽略类型名,然后看const离哪个近,就修饰谁。
如:const [int] *p; //先去掉int,这时const 修饰*p, p是指针,*p是指针指向的对象,不可变
(3) 修饰引用
(4) 修饰函数的返回值
const修饰符也可以修饰函数的返回值,返回值不可被改变,格式如下:
- const int Fun1();
-
- const MyClass Fun2();
(5) 修饰类的成员函数
const修饰符也可以修饰类的成员函数,格式如下:
- class ClassName
- {
- public:
- int Fun() const;
- .....
- };
这样,在调用函数Fun时就不能修改类里面的数据。
- class A
- {
- public:
- A(int i=0):test(i) {}
- private:
- const int i
(6) 在另一连接文件中引用const常量
- extern const int i;
-
- extern const int j=10;
三. 几个要说明的问题
(1) 如何初始化类内部的常量?
方法1:初始化列表:
- class A
- {
- public:
- A(int num):i(num)
- {};
-
- private:
- const int i;
- };
方法2: 外部初始化
- class A
- {
- public:
- A() {}
- private:
- static const int i;
- };
- const int A::i=3;
(2) 放在类内部的常量有什么限制?
- class A
- {
- public:
- A()
- {};
-
- private:
- const int c1 = 7;
- static const int c2 = 7;
-
- static int c3 = 7;
- }
-
-
- class A
- {
- public:
- A(int num):c1(num)
- {};
-
- private:
- const int c1;
- static const int c2;
-
- static int c3;
- }
- const int A::c2 = 7;
- int A::c3 = 7;
这里是三种正确的初始化方法,要注意的是static在外部初始化时,不能再加上static标志。
3) const到底是不是一个重载的参考对象?
先看一个例子:
- class A
- {
- ......
- void f(int i) {......}
- void f(int i) const {......}
- ......
- };
上面是重载是没有问题的了,那么下面的呢?
- class A
- {
- ......
- void f(int i) {......}
- void f(const int i) {......}
- ......
- };
这是个错误的,编译不通过。那么是不是说内部参数的const不予重载呢?
再看下面的例子:
- class A
- {
- ......
- void f(int&) {......}
- void f(const int&) {......}
- ......
- };
这个程序是正确的,看来上面的结论是错误的。为什么会这样呢?这要涉及到接口的透明度问题。
按值传递时,对用户而言,这是透明的,用户不知道函数对形参做了什么手脚,在这情况下进行重载是没有意义的,所以规定不能重载!
当指针或引用被引入时,用户就会对函数的操作有了一定的了解,不再是透明的了,这时重载是有意义的,所以规定可以重载。
c/c++中const详解
原文:http://www.cnblogs.com/honeybusybee/p/5258093.html