C++官方文档-常量成员函数
#include <iostream>
using namespace std; class MyClass
{
public:
int x;
static int n;
const int y;
MyClass(int val)
: x(val), y()
{
}
// int get()
// {
// return x;
// }
//int get() const {return x;} // const member function
//const int& get() {return x;} // member function returning a const&
//const int& get() const {return x;} // const member function returning a const&
int get()
{
return x;
}
int gett() const
{
n++;
return y;
}
int getY()
{
cout<<"非成员函数常量getY"<<endl;
return y;
}
int getY() const
{
cout<<"常量成员函数getY"<<endl;
return y;
}
//return const int&
//只要不修改对象的状态就是合法的
// int& getYYYY()
// {
// //错误,返回对象常量的引用给外部
// return y;
// }
// int& getYYYY() const
// {
// //错误,返回对象常量的引用给外部,改成常量成员函数还是非法
// return y;
// }
//返回常量引用
const int& getXX()
{
return x;
}
//常量成员函数返回常量引用
const int & getXXX() const
{
// 错误,改变了对象的状态
// x++;
return x;
}
//成员函数返回一个常量引用
const int& getYY()
{
//错误
// y++;
// 正确
// x++;
return y;
}
//返回值,合法
int getYYY()
{
return y;
}
//判断是否合法,只要判断是否改变了对象的状态,比如对象的成员是常量,但是返回引用给外部
//常量成员函数是否改变了对象的状态 };
int MyClass::n = ;
/**
* 常量对象只能调用常量成员函数,所以上面的get不能从foo对象调用
*/
/**
*
* Member functions specified to be const cannot modify non-static data members nor call other non-const member functions.
* In essence, const members shall not modify the state of an object.
* 指定成const的成员函数不能修改非静态数据成员,也不能调用非常量成员函数,从本质上来说,常量成员不能修改对象的状态
*/
/**
*const objects are limited to access only member functions marked as const,
*but non-const objects are not restricted and thus can access both const
*and non-const member functions alike.
*常量对象被限制能只允许调用标记成常量的成员函数,但是非常量对象没有这么严格,因此允许它调用常量和非常量成员函数.
*
*/
int main()
{
const MyClass foo();
MyClass bar();
// foo.x = 20; // not valid: x cannot be modified
cout << foo.x << '\n'; // ok: data member x can be read
int y = foo.gett();
cout << "y=" << y << endl;
cout << "n=" << MyClass::n << endl;
cout<<"getY"<<foo.getY()<<endl;
cout<<"getY"<<bar.getY()<<endl;
cout<<MyClass::n<<endl;
return ;
}
C++官方文档-常量成员函数的更多相关文章
- swift官方文档中的函数闭包是怎么理解的?
官方文档中的16页: numbers.map({ (number: Int) -> Int in let result = * number return result }) 不知道这个怎么用, ...
- tensorflow官方文档中的sub 和mul中的函数已经在API中改名了
在照着tensorflow 官方文档和极客学院中tensorflow中文文档学习tensorflow时,遇到下面的两个问题: 1)AttributeError: module 'tensorflow' ...
- 【pytest官方文档】解读- 插件开发之hooks 函数(钩子)
上一节讲到如何安装和使用第三方插件,用法很简单.接下来解读下如何自己开发pytest插件. 但是,由于一个插件包含一个或多个钩子函数开发而来,所以在具体开发插件之前还需要先学习hooks函数. 一.什 ...
- Android的AutoCompleteTextView在API17高版本添加的setText函数在低版本系统居然能正常调用?官方文档是不是不靠谱了?
官方文档:https://developer.android.com/reference/android/widget/AutoCompleteTextView.html#setText(java.l ...
- hbase官方文档(转)
FROM:http://www.just4e.com/hbase.html Apache HBase™ 参考指南 HBase 官方文档中文版 Copyright © 2012 Apache Soft ...
- HBase官方文档
HBase官方文档 目录 序 1. 入门 1.1. 介绍 1.2. 快速开始 2. Apache HBase (TM)配置 2.1. 基础条件 2.2. HBase 运行模式: 独立和分布式 2.3. ...
- Kotlin开发语言文档(官方文档)-- 目录
开始阅读Kotlin官方文档.先上文档目录.有些内容还未阅读,有些目录标目翻译还需琢磨琢磨.后续再将具体内容的链接逐步加上. 文档链接:https://kotlinlang.org/docs/kotl ...
- Hui之Hui.js 官方文档
基础 // 判断值是否是指定数据类型 var result = hui.isTargetType("百签软件", "string"); //=>true ...
- Android 触摸手势基础 官方文档概览
Android 触摸手势基础 官方文档概览 触摸手势检测基础 手势检测一般包含两个阶段: 1.获取touch事件数据 2.解析这些数据,看它们是否满足你的应用所支持的某种手势. 相关API: Moti ...
随机推荐
- 2N7002
就相当于一个开关,当G>S的一定电压就导通,若没有达到就没有导通. 并且注意的是D脚不能直接接电压,中间要接一个电阻. 这用是G输入,S接地,D输出. 跟三极管差不多.分N和P
- scroll滚动动画(js/ts)
//(蓝色this部分为dom) scrollToLeft(option?: { duration?: number, direction?: number }) { let direction = ...
- HDU5373 The shortest problem (YY)
http://acm.hdu.edu.cn/showproblem.php?pid=5373 YY题,模拟下计算过程就好了,计算中并不要保存实际数(这个数会非常大),只要保存到目前为止的数字位上的和 ...
- CH3602 Counting Swaps
题意 3602 Counting Swaps 0x30「数学知识」例题 背景 https://ipsc.ksp.sk/2016/real/problems/c.html Just like yeste ...
- test20181005 序列
题意 考场30分 维护差值,考虑每次移动的变更,当前2-n位置上的差加1,1位置上的差减n-1. 然后要求的是绝对值的和,用吉司机线段树维护最大最小值.次大次小值. 期望复杂度\(O(n \log n ...
- gevent和tornado异步
阅读目录 从 Tornado 说起 再来看下 Gevent 总要总结一下 原文:http://www.pywave.com/2012/08/17/about-gevent-and-tornado/ 还 ...
- pycharm -- 导入主题(theme) and 修改背景颜色(护眼色)
前情提要 众所周知,随着python语言的不断流行,越来越多的程序员开始用python来开发自己的项目以及产品. pycharm作为一款流行的IDE,被越来越多的程序员所接受和使用. 尽管pychar ...
- 【转】每天一个linux命令(18):locate 命令
原文网址:http://www.cnblogs.com/peida/archive/2012/11/12/2765750.html locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其 ...
- docker 知识点
docker 教程:http://www.runoob.com/docker/docker-tutorial.html docker 仓库地址:https://store.docker.com/ do ...
- NPOI控制Excel格式
1.//sheet.SetColumnWidth(3, 50 * 256); 控制第三列宽,单位为1/256个字符 dataRow.Height = 18 * 20; 控制行高,单位为1/20点 s ...