20. 不可修改的对象
来源:互联网 发布:网络销售授权书怎么写 编辑:程序博客网 时间:2024/05/22 10:47
建议:
传对象,需要花费堆栈。
传地址,担心别人修改指针多指向的内容.
1. 传参数 void f(const int *x);
2. const Currency the_raise(42,38); //对象Currency里面的值是不能被修改的. 这样肯定是不行的,建议成员函数加const 相当于this 是const
class Data {}int Data::get_day() const{ day++; //出错, 原因是加了const int Data::get_day() const set_day(12); //出错 原因是加了const int Data::get_day() const}
class A{ int i; public: A():i(0) {} void f() { cout << "f()" << endl; } //void f(A* this) { cout << "f()" << endl; void f() const { cout << "f() const" << endl;} //const其实表示的是this //void f(const A* this) { cout << "f() const" << endl;};int main(){ const A a; a.f(); //f() const return 0;}
class A{ const int i; //必须初始化,方式是在构造函数进行初始化};
class HasArray{ const int size; int array[size]; //error};class HasArray{ enum { size = 100 }; int array[size]; //ok};
阅读全文
0 0
- 20. 不可修改的对象
- NSMutableDictionary对象用set方法初始化的键值真的不可修改吗
- const放在函数后面不可以修改对象的数据
- 不可变的String对象
- 不可变对象的权衡
- String对象的不可变性
- 对于一个不可修改的类,它的每个对象是不是都必须声明成final的?
- 文本编辑框的右键菜单不可修改?
- 设置input的text不可修改
- 关于字符串指针不可修改的问题
- 关于字符串指针不可修改的问题
- 关于字符串指针不可修改的问题
- 关于字符串指针不可修改的问题
- 关于字符串指针不可修改的问题
- python实现不可修改的常量
- char *指向的内容不可修改
- python实现不可修改的常量
- 关于字符串指针不可修改的问题
- Spring配置文件中如何使用外部配置文件配置数据库连接
- Bootstrap4(四)
- Missing Hive Execution Jar: /home/csu/apache-hive-2.1.0-bin/conf/apache-hive-2.1.0-bin/lib/hive-exec
- 区块链开发(十三)开发文档
- Oracle数据库-建库、建表空间,建用户
- 20. 不可修改的对象
- 键盘随意输出任一个数 负数结尾求和
- Ubuntu 16.04 安装配置 JDK 8
- java api 操作 hdfs
- NLTK在python中文字所表达的情感预测
- raspberryPi的omxplayer使用方法
- 617. Merge Two Binary Trees
- Echarts使用
- 基础练习 特殊回文数