共享数据的包含const
2016-03-23 19:37
316 查看
1、常引用:被引用的对象不能被更新 使用:const 类型名 &引用对象 如const int &a;
2、常对象:必须进行初始化,并且对象不能改变 使用:类名 const 对象名 常对象只能访问常成员函数;
3、常数组:数组元素不能改变 使用:const 数组名
4、常指针:指向常量的指针 const char *p
5、常成员函数:在函数体中不能改变对象的数据成员;使用:void A::f() const{}
注意:const可以参与对重载函数的区分;
常引用做形参:
常对象:
常成员函数:
2、常对象:必须进行初始化,并且对象不能改变 使用:类名 const 对象名 常对象只能访问常成员函数;
3、常数组:数组元素不能改变 使用:const 数组名
4、常指针:指向常量的指针 const char *p
5、常成员函数:在函数体中不能改变对象的数据成员;使用:void A::f() const{}
注意:const可以参与对重载函数的区分;
常引用做形参:
#include<iostream> using namespace std; void display(const double& r); int main() { double d(9.5); display(d); return 0; } void display(const double& r) //常引用作形参,在函数中不能更新 r所引用的对象。 { cout<<r<<endl; }
常对象:
class A { public: A(int i,int j) {x=i; y=j;} ... private: int x,y; }; A const a(3,4); //a是常对象,不能被更新
常成员函数:
#include<iostream> using namespace std; class Cuboid { const int length; const int weight; const int height; public: Cuboid(int i,int k,int j); int volume() const;//常成员函数,,,,常对象只能访问常成员函数; }; Cuboid::Cuboid(int i,int k,int j):length(i),weight(j),height(k) {} int Cuboid::volume() const { return length*weight*height; } int main() { Cuboid const cu(12,10,12); cout<<"volume = "<<cu.volume()<<"\n"; }
相关文章推荐
- Common Lisp 笔记(1)
- jQuery学习笔记(一)
- linux中libevent-2.0.so.5 not found 解决 ldd 方法
- 第4周项目5(4) 用递归方法求解
- Delphi 中 FindWindow 和 FindWindowEx 找到外部进程,然后发送消息(比如最大化)
- 记录Hibernate的缓存知识
- Linux内核分析第五周学习总结:扒开系统调用的三层皮(下)
- Linux内核分析作业 NO.5
- 比特承诺的概念
- 第4周项目3:随机数函数应用于游戏-猜数字游戏
- fast-rcnn配置运行demo.py(Ubuntu14.04),不成功的朋友请与我(lee)联系,后面附带邮箱
- 张量的展开与matlab下的工具包操作
- Linux内核分析第五章读书笔记
- 关闭磁感应,光感,,
- App架构经验总结
- 学习Mybatis:Mybatis的使用
- Linux内核分析5:分析system_call中断处理过程
- 网易2016机器翻译实习生招聘笔试编程题
- HDU 2178 猜数字
- 第4周项目3-(1)猜数字游戏