C++中const
【const】
0.普通const对象定义在栈空间中
{
int a = ;
const int b = ;
cout << &a << ' ' << &b;
}
Result:
0x22abec 0x22abe8
注:Code::Blocks + Cygwin测得。
1.对变量类型加以限定,使之不能被修改;
const int e1 = 100;
e1 = 101; // error
2.const对象必须被初始化;
const int e2; // error
3.const对象和non-const对象可以通过彼此进行赋值;const对象也可以通过表达式进行初始化;
int get_size() { return ; }
int main()
{
int i = ;
const int a = i; // non-const to const
const int b = a; // const to const
int c = a; // const to non-const
int d = i; // non-const to non-const
int e = get_size();
return ;
}
4.默认情况下,const对象仅在文件内有效(From定义之后To文件末尾);
5.要在多个文件内共享一个const对象,需要使用extern关键字;
《C++ Primer》第五版的说法有一点问题,即“对于const变量不管是声明还是定义都添加extern关键字,这样只需定义一次就可以了”。
经过测试:可以在定义时不添加extern关键字,即下面的代码是可行的:
// file1.cpp
const int a = ; // file2.cpp
extern const int a;
但在声明和定义时都添加extern关键字应该是“最佳实践”,即在定义时就说明此const变量为整个工程可见。
6.const引用
6.1 不能通过const引用修改对象的值,无论其指向的对象是否为const对象;
6.2 非const引用不能指向一个const对象;
{
int i = ;
const int a = i;
int& b = i;
int& c = a; // error, 6.2
const int& d = i;
d = ; // error, 6.1
const int& e = a;
e = ; // error, 6.1
}
6.3 将一个const引用绑定到另外一种类型上发生了什么:
{
double dval = 3.14;
const double& rd = dval;
const int& ri = dval;
cout << &dval << ' ' << &rd << endl;
cout << &dval << ' ' << &ri << endl;
}
Result:
0x22abd8 0x22abd8 // ok
0x22abd8 0x22abe4 // wrong!
整形const引用的地址和dval的地址不一致。《C++ Primer》解释说,这是因为类型转换发生时,ri指向的是一个临时变量,并且,C++将这种行为归为非法。
但却能run起来。
7.指向和const
7.1 指向const对象的指针(pointer to const):a.禁止指向非const对象的指针 指向 const对象;b.禁止将 指向const的指针 赋值给 指向非const的指针;
7.2 不能更改指向const对象的指针的值;
{
const double cpi = 3.14;
double *ppi = &cpi; // error, 7.1.a
const double *cppi = &cpi;
double pi = 3.14;
double *ppi2 = π
*ppi2 = 1000.0;
const double *cppi2 = π
ppi2 = cppi2; // error, 7.1.b
*cppi2 = 100.0; // error, 7.2
}
7.3 常量指针(const pointer):必须初始化、一旦完成初始化就不能更改、若所指对象非const,则可以修改其值;
{
int a = ;
int b = ;
int *p1 = &a;
*p1 = ;
p1 = &b;
int* const cp2 = &b;
*cp2 = ; // ok
cp2 = &b; // error, pointer is const
int* const cp3; // error, uninitialized
}
8.顶层const,底层const
顶层const:对象本身是否为常量,整数本身、对象本身、指针本身,即自己,不能改变自己;
底层const:指针 所指对象是否为常量,不能改变指针 所指对象 的值;
const int * const pi = &a; // 1.前一个const为底层const,后一个const为顶层const;2.从右往左阅读法;
9.类与const
9.1 const成员函数
作用:修改隐式this指针的类型。添加const之后,this指针所指对象的普通数据成员(非static非mutable)不能被更改。
类型转换实值:将 T* const this 更改为 const T* const this,此时,this指针所指对象可以是const对象。
9.2 const对象,以及const对象的引用或指针都只能调用const成员函数;
struct X {
X(int x, int y) : ex(x), ey(y) {};
void display1() { cout << ex << endl; }
void display2() const { cout << ey << endl; }
int ex;
int ey;
};
int main()
{
X x(, );
x.display1();
x.display2();
const X cx(, );
cx.display1(); // error, display1为非const成员函数
cx.display2();
return ;
}
9.3 若成员函数为const,则其声明和定义都必须添加const(与上面说的extern的用法不同);
9.4 成员函数可以根据const属性进行重载;
struct X {
void display() {}
void display() const {}
};
9.5 const成员变量:a.不能被修改;b.能进行类内初始化;c.若已类内初始化,则可以使用默认构造函数;d.可以在构造函数初始化列表中初始化;
struct X {
X() {}; // 默认构造函数
X(int x, int y) : ex(x), ey(y) {};
void display1() { cout << ex << endl; }
void display2() const;
const int ex = ; // 类内初始化
const int ey = ; // 类内初始化
};
void X::display2() const { cout << ey << endl; }
若未进行类内初始化,则默认构造函数不能通过编译,即当上面代码的8、9行没有赋值时,上面的默认构造函数不能通过编译,无论是否用到此默认构造函数;
若未进行类内初始化,又需要使用默认构造函数,可以在默认构造函数的初始化列表中给const成员变量赋值:
X() : ex(), ey() {};
其中的99、100可以更改为其它表达式:
int out1 = ;
struct X {
X() : ex(out1), ey(out1+) {}
...
};
-------------------------------------------------------------------
其它:
1.用于声明引用的const都是底层const,即不存在顶层const
const int a = 100;
const int & const b = a; // error: 'const' qualifiers cannot be applied to 'const int&'
2.引用可以赋值给引用
int a = 10;
int& b = a;
int& c = b; // ok, b is a reference
但const引用不能赋值给非const引用
const int& c = a;
int& d = c; // error
C++中const的更多相关文章
- C、C++中const的区别
C语言中: 被const修饰的变量,仍然是变量.虽然不能用C语法给这个变量改变值,但他本质上还是变量. C编译器会给它分配空间. C中,const默认使用的是外部链接. C++中: 被const修饰的 ...
- JavaScript中const、var和let区别浅析
在JavaScript中有三种声明变量的方式:var.let.const.下文给大家介绍js中三种定义变量的方式const, var, let的区别. 1.const定义的变量不可以修改,而且必须初始 ...
- C/C++中const的用法 分类: C/C++ 2015-07-05 00:43 85人阅读 评论(0) 收藏
const是C语言的关键字,经C++进行扩充,变得功能强大,用法复杂.const用于定义一个常变量(只读变量),当const与指针,引用,函数等结合起来使用时,情况会变得复杂的多.下面将从五个方面总结 ...
- C++中const 的各种用法
C++中const 关键字的用法 const修饰变量 const 主要用于把一个对象转换成一个常量,例如: ; size = ; // error: assignment of read-only v ...
- (转) C/C++中const关键字详解
文章转自 http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777416.html 为什么使用const?采用符号常量写出的代码更容易维 ...
- 实例讲述PHP面向对象的特性;;;php中const与define的使用区别
php中const与define的使用区别 1.const:类成员变量定义,一旦定义且不能改变其值. define:定义全局常量,在任何地方都可以访问.2.define:不能在类中定义,而const可 ...
- C++中const简介及用法
1.const简介 C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮助. Const 是C++中常用的类型修饰 ...
- C/C++ 中 const 修饰符用法总结
C/C++ 中 const 修饰符用法总结 在这篇文章中,我总结了一些C/C++语言中的 const 修饰符的常见用法,供大家参考. const 的用法,也是技术性面试中常见的基础问题,希望能够帮大家 ...
- C++中const用法详解
本文主要内容来自CSDN论坛: http://bbs.csdn.net/topics/310007610 我做了下面几点补充. 补充: 1. 用const声明全局变量时, 该变量仅在本文件内可见, 类 ...
- C++ 中 const和define的区别
来源网址:http://wujiangping.blog.163.com/blog/static/195182011201255115125205/ 请区别用#define命令定义的符号常量和用con ...
随机推荐
- C#检测串口被拔掉等一些触发事件合集
// //设备异常重载 // protected override void WndProc(ref Message m) { if (m.Msg == 0x0219) {//设备被拔出 if (m. ...
- Kendo Web UI Grid数据绑定,删除,编辑,并把默认英文改成中文
Kendo Web UI 是个不错的Jquery框.可惜老外写的,很多都是默认的英文,当然我们也可以设置成中文,接下来,我们就看看Grid是如何实现的数据绑定(Kendo Grid数据绑定实现有很多方 ...
- lnmp安装fileinfo扩展
1.错误: PHP Fileinfo extension must be installed/enabled to use Intervention Image. 2.原因: 缺少 fileinfo扩 ...
- Python-----格式化字符
摘要: Python中 %s . %r Python中也有类似于C中的 printf()格式输出,使用 % 运算符,格式: 格式标记字符串 % 要输出的值组 右边的”值组“若有两个及以上的值则需要用小 ...
- Truck History--poj1789
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21534 Accepted: 8379 De ...
- js 对url字符转译全解
1.js 对url进行字符解码设计到3个方法 escape , encodeURI , encodeURIComponent eg: var url='http://baidu.com';encode ...
- 在VS里面查看lua堆栈
extern std::string get_lua_stack(void); std::string stack = get_lua_stack(); std::string get_lua ...
- BZOJ 1877 晨跑
http://www.lydsy.com/JudgeOnline/problem.php?id=1877 思路:拆点费用流,答案就是最大流量和最小费用. #include<algorithm&g ...
- VC运行库版本不同导致链接.LIB静态库时发生重复定义问题的一个案例分析和总结
转帖:http://blog.csdn.net/whygosofar/article/details/2821875 MSDN中对于在不同的配置下Link的LIB作了说明: C Runtime Lib ...
- centos 6.5 hadoop 2.3 初配置
为了安装hadoop废了好大的劲才把esxi5.5给装好. 同时装了centos6.5,由于hadoop里面有个免密码登陆所以这里讲的就是免密码登陆. 看了大家的博客文章发现转发的一部分,写ubunt ...