const修饰的类成员函数
来源:互联网 发布:mac装win10鼠标不能用 编辑:程序博客网 时间:2024/06/04 19:41
1、在类中用const修饰的成员函数有一个作用就是不能在该函数中修改类数据成员的值。
class Test{public:void fun(int x) const{x = 2;a = 3; //errorb = 4; //error}private:int a;int b;};
2、对于const修饰的类对象,只能调用const修饰的成员函数,而不能调用非const成员函数。
#include <iostream>using namespace std;class Test{public:void fun(int x) const{x = 2;}void fun2(){}private:int a;int b;};int main(){const Test t;t.fun(1);t.fun2(); //errorreturn 0;}
而对于非const修饰的类对象则可以调用任何public成员函数。如果是一对有无const的重载成员函数,非const类对象会调用非const成员函数。
0 0
- const修饰的类成员函数
- const修饰类的成员函数
- const修饰类的成员函数
- const修饰类的成员函数
- const修饰类的成员函数
- const修饰类的成员函数
- const修饰的成员函数
- const 之 const修饰的成员函数
- const修饰成员函数
- const修饰成员函数
- const 修饰成员函数
- const修饰成员函数
- const 修饰的常函数成员
- const修饰成员函数const修饰对象
- const修饰成员函数总结
- C++ const 修饰成员函数
- C++ const 修饰成员函数
- C++类数据成员和成员函数const修饰
- 可以转换为多种文件格式的PDF转换器
- iOS 7中UIImagePickerController拍照或录制视频黑屏的问题
- StringBuffer和StringBuilder用法比较
- libsvm工具箱C++下编程实践2
- IO流 【java笔记】
- const修饰的类成员函数
- Android Browser学习三 多窗口: 展示第一个Tab的过程
- iOS开发 导航栏 UINavigationBar 属性
- 面试算法题
- Android Design - 设计风格
- POJ 2240:Arbitrage:folyd最短路算法变形求有向图的盈利环存在
- third_party/
- 优秀开源项目网站分享
- POJ 1017 Packets (贪心)