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 ...
随机推荐
- 值得收藏的批处理程序 - imsoft.cnblogs
文件强力删除.bat @echo off @echo 文件马上被强制删除 @echo 确定吗? pause DEL /F /A /Q \\?\%1 RD /S /Q \\?\%1 虚拟Wi-Fi.ba ...
- mysql-python安装时mysql_config not found
在安装 mysql-python时,会出现: sh: mysql_config: not found Traceback (most recent call last): File "s ...
- hdu2098 分拆素数和 素数筛
将一个偶数拆成两个素数的和,欧拉筛暴力 #include<stdio.h> #include<string.h> #define N 10001 ]; ]; ,j; void ...
- CH4201 楼兰图腾
题意 4201 楼兰图腾 0x40「数据结构进阶」例题 描述 在完成了分配任务之后,西部314来到了楼兰古城的西部.相传很久以前这片土地上(比楼兰古城还早)生活着两个部落,一个部落崇拜尖刀('V'), ...
- POJ2311 Cutting Game
题意 Language:Default Cutting Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6007 Acc ...
- LG1419 【寻找段落】
前言 鉴于这题的题解质量(连LaTeX公式都没有),我决定再发一篇详细的题解,不仅方便大家,还可以作为我学习单调队列优化dp的小结(尽管这题不是dp). 分析 题目要求求一个最大的实数\(x\),使得 ...
- 移动端 元素外面使用伪类after加边框 导致其内部元素无法选中
解决方法:给内部元素增加属性 position: relative; z-index: 3; 这样就能选中其内部元素了.
- 实习第一周第一天:接口 extends是继承类,implement是实现接口,原接口里面的方法填充,方法名也是不变,重写override是父类的方法名不变,把方法体给改了
一.定义 Java接口(Interface),是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为( ...
- JSON字符串-赋张最初接触后台从map转json的方法
**************************************** json数组: *************************************************** ...
- UltraEdit常用设置及快捷键
= 关闭自动加载上次文件的方法,操作方法如下:首先,要打开UltraEdit,然后点击经[高级]-[配置],找到[文件处理]-[加载],把[重新载入先前在启动时打开的文件]勾去掉,并确定就可以了. 附 ...