const 无处不在!

来源:互联网 发布:手机搜索软件 编辑:程序博客网 时间:2024/06/08 11:22

在C++的函数定义中,到处都是const关键字的使用
1.const修饰普通变量
2.const修饰指针
3.const修饰函数参数
4.const修饰函数返回值
5.const在类中的使用
For example,下面这段代码将const的各种应用场景都糅杂到了一起:
#include

using namespace std;

/*
* const修饰函数参数
*
* const int i1:参数是常量,不允许在函数中修改这个参数
* const int* i2:参数指针所指的内容为常量不可变
* int* const i3:参数指针本身是常量,不可变
* const int& i4:参数为引用,增加效率(不用拷贝参数)同时防止修改
*
* 其实和const修饰变量是一样的效果
*/
void func_param( const int i1,
const int* i2,
int* const i3,
const int& i4 ){

cout << “func_param_i1: ” << i1 << endl;
//compile error: assignment of read-only parameter ‘i1’
//i1 = 10;

cout << “func_param_i2: ” << *i2 << endl;
//compile error: assignment of read-only location ‘* i2’
//*i2 = 10;
i2 = NULL;

cout << “func_param_i3: ” << *i3 << endl;
*i3 = 10;
//compile error: assignment of read-only parameter ‘i3’
//i3 = NULL;

cout << “func_param_i4: ” << i4 << endl;
//compile error: assignment of read-only reference ‘i4’
//i4 = 10;
}

/*
* const修饰函数返回值
* const int func_ret_1(),没啥意义,因为返回值本身就是赋值
* const int* func_ret_2(),把func_ret_2()看成一个变量,指针内容不可变
* int const* func_ret_3(),把func_ret_3()看作成一个变量,即指针本身不可变
*/
const int func_ret_1(){
return 1;
}
const int* func_ret_2(){
int i = (int)malloc(sizeof(int));
*i = 100;
return i;
}
int* const func_ret_3(){
int i = (int)malloc(sizeof(int));
*i = 100;
return i;
}

/*
* const在类中的应用
*/
class TestConst{
/*
* const修饰类成员“变量”
*/
private:
/*
* 成员变量不能修改
* 只能在初始化列表中赋值
*/
const int iConst;
public:
/*
* 在初始化列表中对const成员赋值
*/
TestConst(int x): iConst(x) { cout << “TestConst Create” << endl; }
~TestConst() { cout << “TestConst Destroy” << endl; }

/*
* const修饰类成员函数
*/
private:
//普通变量
int iVar;
//非const成员函数
int testFunc() { return 1; }
public:
/*
* 常成员函数,它不能改变对象的成员变量
* 也不能调用类中任何非const成员函数
*/
void testConstFunc() const {
//compile error: assignment of member ‘TestConst::iVar’ in read-only object
//iVar = 10;
//compile error: passing ‘const TestConst’ as ‘this’ argument discards qualifiers [-fpermissive]
//testFunc();
}
};

int main()
{
cout << “main begin” << endl;

/*
* const定义一个常量,其值不可变
*/
cout << “define a const variable” << endl;
const int constInt = 10; //等价于这样的定义:int const constInt = 10;
//compile error: assignment of read-only variable ‘constInt’
//constInt = 100;

/*
* const修饰一个指针会怎么样呢
* 若const位于*左侧,则const用于修饰指针所指向的内容,即指针指向一个常量
* 若const位于*右侧,则const用于修饰指针本身,即指针本身是一个常量
*/
cout << “define a variable” << endl;
int varInt = 10;

/*
* const位于*右侧,指针本身是常量
*/
int* const p1 = &varInt;
*p1 = 100;
//compile error: assignment of read-only variable ‘p1’
//p1 = NULL;

/*
* const位于*左侧,指针指向一个常量
*/
const int* p2 = &varInt;
//compile error: assignment of read-only location ‘* p2’
//*p2 = 100;
p2 = NULL;

/*
* const位于*左侧和右侧,指针本身、指向的内都是常量
*/
const int* const p3 = &varInt;
//compile error: assignment of read-only location ‘(const int)p3’
//*p3 = 100;
//compile error: assignment of read-only variable ‘p3’
//p3 = NULL;

/*
* 测试const修饰函数参数
*/
int param = 100;
func_param(param, &param, &param, param);

/*
* 测试const修饰函数返回值
*/
int ret1 = func_ret_1();

//compile error: invalid conversion from ‘const int*’ to ‘int*’ [-fpermissive]
//int *ret2 = func_ret_2();
//*ret2 = 100;
//ret2 = NULL;

int *ret3 = func_ret_3();
*ret3 = 100;
ret3 = NULL;

/*
* 测试类中的const
*/
TestConst *t = new TestConst(10);
delete t;

cout << “main end” << endl;
return 0;
}

原创粉丝点击