C++ [ 共享数据保护 ]
来源:互联网 发布:ubuntu iso镜像安装 编辑:程序博客网 时间:2024/05/18 02:47
1、常引用:所引用的对象不能更新
const 类型说明符 &引用名;
#include <iostream.h>
void display(const double& r);
int main(void){
double d(9.5);
display(d);
return 0;
}
void display(const double& r){
/*
r = 10;
error C2166: l-value specifies const object
*/
//常饮用做形参,函数中不能更新r所引用的对象
cout<<r<<endl;
}
2、常对象:必须进行初始化,而且不能被更新
类名 const 对象名;
#include <stdlib.h>
using namespace std;
class A{
public:
A(int i, int j){x=i;y=j;}
void setX(int x){x=x;}
void setX(int x) const {x=x;}
private:
int x,y;
};
int main(int argc, char *argv[])
{
int const n=10;
//a是常对象,不能被更新
A const a(3,4);
/*
a.setX(5);
'const A' as 'this argument of 'void A::setX(int)' discards qualifiers
*/
system("PAUSE");
return 0;
}
3、常成员函数
类型说明符 函数名(参数表) const;
常对象可以调用常成员函数
4、常数据成员
构造函数对数据成员进行初始化;静态常数据成员在类外说明和初始化
#include <iostream>
#include <stdlib.h>
using namespace std;
class A{
public:
A(int i);
void print();
const int& r;
private:
const int a;
static const int b;
};
const int A::b=10;
A::A(int i):a(i),r(a){
}
void A::print(){
cout<<a<<":"<<b<<":"<<r<<endl;
}
int main(int argc, char *argv[])
{
A a1(100);
a1.print();
system("PAUSE");
return 0;
}
- C/C++,常对象 - 保护共享数据
- C++:数据的共享与保护
- 共享数据的保护
- C++ [ 共享数据保护 ]
- 保护共享数据
- 数据共享与保护
- 数据的共享与保护
- const 数据共享与保护
- 数据的共享与保护
- C++数据共享与保护
- 数据的共享与保护
- C++----共用数据保护
- C/C++基础知识总结——数据的共享与保护
- 在并发例程中保护共享数据
- 共享存储容灾数据保护 RAID
- 单元模型对象必须保护共享数据
- C++数据的共享与保护
- c++笔记--共享数据的保护
- JAVA简单有趣
- 视频相关的几个开源软件的授权
- Android上的Google Map 开发总结
- 2011.9.26 多云
- windows live messenger 2009登录失败,提示“错误代码:81000605”的一个解决办法
- C++ [ 共享数据保护 ]
- 如何优化代码节约系统资源解决重复实例化对象的问题——神奇的单例模式(C#设计模式)
- Android-MMS中彩信附件的格式及分析
- Sharepoint 弹出消息提示框
- QT编写中碰到:找不到批处理文件
- Android之Toast的用法
- log4j 配置
- 学习重要,还是经营人脉重要?(转自李笑来)
- Javascript的Defer属性