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 ...
随机推荐
- eclipse 配置jdk和maven
准备工作:确保已安装好jdk和maven,并完全配置环境.若是没有请参考前两篇博客: jdk: http://www.cnblogs.com/qinbb/p/6861851.html maven ...
- Knight Tournament 合并区间
Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the me ...
- 《DSP using MATLAB》Problem 4.16
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- test20180921 手机信号
题意 分析 我们用形如(l, r, v) 的三元组描述一个区间,这个区间中从l 到r 每隔v 有一个信号站. 考虑一次construct 操作,会添加一个新的区间,并可能将一个已经存在的区间分裂为两个 ...
- harbor helm 仓库使用
harbor 已经支持helm 私服仓库了,还是比较方便的 安装 下载在线安装包 wget https://storage.googleapis.com/harbor-releases/release ...
- 【VB.NET】——若水归海
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/successA/article/details/34496209 看完台湾微软特约资深讲师的VB.N ...
- jquery ajax 上传文件
html:<!-- /.tab-pane --> <div class="tab-pane" id="head_portrait"> & ...
- Centos 6.5 yum 安装Apache软件
首先在系统上面查询一下是否已经安装了apache 软件[Apache软件在linux系统里的名字是httpd] rpm -qa httpd 如果有返回的信息,则会显示已经安装的软件.如果没 ...
- AppBox中main树节点单击事件JS(还有叶子的节点的页面链接)
AppBox中main.aspx.csif (menu.IsTreeLeaf) { node.Leaf = true; ...
- 【shell】正则表达式语法
一个正则表达式就是由普通字符(例如字符 a 到 z)以及特殊字符(称为元字符)组成的文字模式.该模式描述在查找文字主体时待匹配的一个或多个字符串.正则表达式作为一个模板,将某个字符模式与所搜索的字符串 ...