C++,常成员函数
2024-08-31 04:12:12
#include <iostream>
using namespace std; class Test {
public:
int x;
int y;
void const_m1() const;
void const_m2() const;
void m3();
void m4();
};
/**
* //1常成员函数不能修改成员变量的值
* //2常成员函数只能调用常成员函数,不能调用普通成员函数
* //3普通成员函数可以调用常成员函数
*/
void Test::const_m1(void)const {
cout<<"start of const_m1() ,call:"<<endl;
this->const_m2();//可以调用const_m2()
//this->m3();//error C2662: “Test::m3”: 不能将“this”指针从“const Test”转换为“Test &”
//this->m4();//error C2662: “Test::m4”: 不能将“this”指针从“const Test”转换为“Test &”
//this->x = 3;//error C3490: 由于正在通过常量对象访问“x”,因此无法对其进行修改
cout<<"end of const_m1()"<<endl;
}
void Test::const_m2(void)const {
cout<<"this is const_m2()"<<endl;
}
void Test::m3(void) {
cout<<"this is m3(),call:"<<endl;
this->const_m2();
cout<<"end of m3()"<<endl;
}
void Test::m4(void) {
cout<<"this is m4()"<<endl;
this->x = ;
} int main(void) {
Test demo;
demo.const_m1();
demo.const_m2();
demo.m3();
demo.m4();
while();
return ;
}
/*测试结果:
start of const_m1() ,call:
this is const_m2()
end of const_m1()
this is const_m2()
this is m3(),call:
this is const_m2()
end of m3()
this is m4()
*/
最新文章
- Fiddler (三) Composer创建和发送HTTP Request
- Codevs 1051 二叉树最大宽度和高度
- jquery.form.js详细讲解
- ren
- This version of MySQL doesn’t yet support ‘LIMIT &; IN/ALL/ANY/SOME 错误解决
- jsp 是什么 ,jsp 隐式对象
- CentOS 6.6安装LAMP和Subversion服务器
- 协议Protocol
- c#基础精华01(强调代码规范,虚方法,抽象方法,接口)
- 可恶的QT隐式共享
- 完全不借助VS,编写C#控制台应用程序
- SQL Server常用脚本
- java list基本用法
- java多线程——同步块synchronized详解
- USACO Ski Course Design 暴力
- php+redis 学习 四 队列
- unittest测试用例的执行顺序
- Windows下安装Python虚拟环境
- C#中用DateTime的ParseExact方法解析日期时间(excel中使用系统默认的日期格式)
- Spring实战拆书--SpringBean
热门文章
- gcc 的编译过程
- BZOJ 1878: [SDOI2009]HH的项链( BIT )
- BZOJ 1044: [HAOI2008]木棍分割(二分答案 + dp)
- Servlet url-pattern优先级
- php 计算多维数组中所有值的总和
- Centos 安装docker报错
- 调不尽的内存泄漏,用不完的Valgrind
- Delphi 类型转换函数(有几个函数没见过,FloatToStrF,FloatToText等等)
- poj 1386 Play on Words(有向图欧拉路+并查集)
- Node.cluster