C++ const 修饰类的用法
来源:互联网 发布:淘宝试用黑名单恢复 编辑:程序博客网 时间:2024/05/29 10:16
C++ const 修饰类用法
1.修饰数据成员
初始化位置只能在参数列表里面
被const修饰的数据成员不能被修改
2.修饰成员函数
位置 函数申明之后,实现体之前
意义: const承诺不会修改数据成员
能访问const和非const数据成员 但不能修改 非const数据成员
只能访问const成员函数
构成重载
const对象只能调用const成员函数。
非const成员对象,优先调用非const成员函数,若无则可调用const成员函数
3.修饰类对象
const 修饰函数,是从函数层面,不修改数据
const修饰对象,是从对象层面,不修改数据,只能调用const成员函数
#include <iostream>using namespace std;class A{public:A(int v):val(v){}void dis() const {cout << val << endl;// x = 200; 不可以 /***********/int a; a = 100; //可以 /***********/ // print(); 也不可以 }void print(){x = 100;}/******构成重载*******/void play(){cout << "void play()" << endl;}void play()const{cout << "void play() const" << endl;}void plays()const{cout << "void play() const" << endl;}private:const int val ;// 新版可以这样初始化 const int val = 10;int x, y;};int main(){//const int i; 必须初始化 A a(5);a.dis();a.play();a.plays(); //非const成员对象,优先调用非const成员函数,若无则可调用const成员函数 const A b(5);b.play(); //const对象只能调用const成员函数system("pause");return 0;}
0 0
- const 修饰类函数的用法
- C++ const 修饰类的用法
- C/C++中const修饰符的用法总结
- const修饰的pointer用法
- const修饰的部分用法
- const修饰指针的用法
- C语言const的修饰
- extern,static,const修饰变量的用法
- const限定修饰符的各种用法
- c++ const的其他修饰用法
- const修饰指针和引用的用法
- const修饰指针和引用的用法
- const修饰指针和引用的用法
- const修饰指针和引用的用法
- const修饰指针和引用的用法
- C:带有const修饰的指针解读
- C:带有const修饰的指针解读
- 修改C中const修饰的变量
- The node /hbase-unsecure is not in ZooKeeper.
- 过河问题_纪中2558_递推
- BZOJ 2669 局部极小值 CQOI2012
- css+jquery步骤进度条
- CodeForces 471B MUH and Important Things
- C++ const 修饰类的用法
- Lucene学习总结之三:Lucene的索引文件格式(3)
- java中引用指向问题
- 微软面试100题2010年版全部答案集锦(含下载地址)
- java保留两位小数
- android游戏背景音乐
- #203 Segment Tree Modify
- 20160813 yocsef上海分论坛
- < >