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 ...
随机推荐
- ACdream - 1735:输油管道
Time Limit: 2000/1000MS (Java/Others) Memory Limit: 262144/131072KB (Java/Others) Problem Descriptio ...
- HDU 4647 Another Graph Game 想法类
解题思路:若没有边权,则对点权从大到小排序即可.. 考虑边,将边权拆成两半加到它所关联的两个点的点权中即可. ..因为当两个人分别选择不同的点时,这一权值将互相抵消. 以上摘自杭电的解题报告. 至于为 ...
- [UE4]虚幻4的智能指针
虚幻自己实现了一套智能指针系统,为了跨平台. 指针: 占用8个字节,4个字节的Object指针,4字节的引用计数控制器的指针, 引用计数控制器需要12字节, 一个C++的Object指针4字节,一个共 ...
- test20181015 B君的第二题
题意 分析 考场85分 用multiset暴力,由于教练的机子飞快,有写priority_queue水过了的人. #include<cstdlib> #include<cstdio& ...
- PYTHON之MOCK WEB接口
在日常的测试工作中,有时会有需要调用外部接口,拿到返回数据用以满足当前的测试任务的需求.但是当外部接口不可用,或者没有提供测试用环境时,我们就需要自己来mock一个接口的返回内容了,先让我们看一看下面 ...
- pykd试用
啥是pykd? 一个windbg插件,能在windbg里面运行python指令 试用步骤 下载from https://pykd.codeplex.com/releases/view/615625 解 ...
- 【转】每天一个linux命令(19):find 命令概览
原文网址:http://www.cnblogs.com/peida/archive/2012/11/13/2767374.html Linux下find命令在目录结构中搜索文件,并执行指定的操作.Li ...
- Lock 和 synchronized 的区别
Lock 和 synchronized 的区别 Lock是一个接口,而synchronized是Java中的关键字,synchronized是内置的语言实现: synchronized在发生异常时,会 ...
- UOJ 188 【UR #13】Sanrd——min_25筛
题目:http://uoj.ac/problem/188 令 \( s(n,j)=\sum\limits_{i=1}^{n}[min_i>=p_j]f(j) \) ,其中 \( min_i \) ...
- 【python】实例-把两个无规则的序列连接成一个序列,并删除重复的元素,新序列按照升序排序
list_one=[3,6,2,17,7,33,11,7] list_two=[1,2,3,7,4,2,17,33,11] list_new=list_one+list_two list=[] i=0 ...