函数声明后面的const用法 void function() const{}
来源:互联网 发布:西西软件家园 编辑:程序博客网 时间:2024/05/16 04:53
通常我们会看到一些函数声明后面会跟着一个const,这个const是做什么的呢?
看一下下面的例子,就知道了。直接在编译前,就会提示下面的两个错误
// test1107.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>using namespace std;class aa{ int num;public: aa(){ int b =10; num = b; }; void out1(){ cout<<num<<endl; } void out2() const{ cout<<num<<endl; } void out3() const{ num+=10; //出错,const函数不能修改其数据成员 cout<<num<<endl; }};int _tmain(int argc, _TCHAR* argv[]){ aa a1; a1.out1(); a1.out2(); a1.out3(); const aa a2; a2.out1(); // 错误,const的成员 不能访问非const的函数 a2.out2(); a2.out3(); return 0;}
在类成员函数的声明和定义中,
const的函数不能对其数据成员进行修改操作。
const的对象,不能引用非const的成员函数。
http://www.cnblogs.com/xing901022/p/3413019.html
阅读全文
0 0
- 函数声明后面的const用法 void function() const{}
- 函数声明后面的const用法
- 函数声明后面的const用法
- 函数声明后面的const用法
- 函数声明后面的const用法
- 函数声明后面的const用法
- C++ 函数声明后面的const用法
- 函数声明后面的const用法
- 函数声明后面的const用法
- 为什么在函数的声明后面还有一个 const? 如:void fun() const;
- 为什么在函数的声明后面还有一个 const? 如:void fun() const;
- 为什么在函数的声明后面还有一个 const? 如:void fun() const;
- 为什么在函数的声明后面还有一个 const? 如:void fun() const;
- 函数声明后面的const是什么意思
- Const用法总结:Const,Const函数,Const变量,函数后面的Const (转)
- Const用法总结:Const,Const函数,Const变量,函数后面的Const
- 函数后面加const的用法
- 关键字:Const,Const函数,Const变量,函数后面的Const
- 给深度学习入门者的Python快速教程
- 新账号重新开始
- 购物车的原理及实现.(仿京东实现原理)
- [monitor] 4. Linux内核Oops机制
- Linux下的eeprom读写操作
- 函数声明后面的const用法 void function() const{}
- Linux vdso的系统调用
- Eclipse 设置编码格式为UTF-8
- float double 指定小数位数输出
- [leetcode] 35. Search Insert Position
- CSS布局系统
- web功能测试-输入框
- 百度地图调用
- iOS蓝牙开发CoreBluetooth框架总结