#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++官方文档-常量成员函数的更多相关文章

  1. swift官方文档中的函数闭包是怎么理解的?

    官方文档中的16页: numbers.map({ (number: Int) -> Int in let result = * number return result }) 不知道这个怎么用, ...

  2. tensorflow官方文档中的sub 和mul中的函数已经在API中改名了

    在照着tensorflow 官方文档和极客学院中tensorflow中文文档学习tensorflow时,遇到下面的两个问题: 1)AttributeError: module 'tensorflow' ...

  3. 【pytest官方文档】解读- 插件开发之hooks 函数(钩子)

    上一节讲到如何安装和使用第三方插件,用法很简单.接下来解读下如何自己开发pytest插件. 但是,由于一个插件包含一个或多个钩子函数开发而来,所以在具体开发插件之前还需要先学习hooks函数. 一.什 ...

  4. Android的AutoCompleteTextView在API17高版本添加的setText函数在低版本系统居然能正常调用?官方文档是不是不靠谱了?

    官方文档:https://developer.android.com/reference/android/widget/AutoCompleteTextView.html#setText(java.l ...

  5. hbase官方文档(转)

    FROM:http://www.just4e.com/hbase.html Apache HBase™ 参考指南  HBase 官方文档中文版 Copyright © 2012 Apache Soft ...

  6. HBase官方文档

    HBase官方文档 目录 序 1. 入门 1.1. 介绍 1.2. 快速开始 2. Apache HBase (TM)配置 2.1. 基础条件 2.2. HBase 运行模式: 独立和分布式 2.3. ...

  7. Kotlin开发语言文档(官方文档)-- 目录

    开始阅读Kotlin官方文档.先上文档目录.有些内容还未阅读,有些目录标目翻译还需琢磨琢磨.后续再将具体内容的链接逐步加上. 文档链接:https://kotlinlang.org/docs/kotl ...

  8. Hui之Hui.js 官方文档

    基础 // 判断值是否是指定数据类型 var result = hui.isTargetType("百签软件", "string"); //=>true ...

  9. Android 触摸手势基础 官方文档概览

    Android 触摸手势基础 官方文档概览 触摸手势检测基础 手势检测一般包含两个阶段: 1.获取touch事件数据 2.解析这些数据,看它们是否满足你的应用所支持的某种手势. 相关API: Moti ...

随机推荐

  1. unknown error: call function result missing 'value'

    好好的脚本全部报错,遇到这种现在看一下自己Chrome浏览的版本号 然后去chromedriver官网上下载最新驱动文件 https://sites.google.com/a/chromium.org ...

  2. HDU 1872:稳定排序

    稳定排序 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  3. Linux操作系统网络配置

  4. PHP独立环境搭建细节

    一.安装前准备: 准备安装软件此处以以下软件为例: Appache:httpd-2.2.21-win32-x86-openssl-0.9.8r.msi MySQL: mysql-5.5.21-win ...

  5. BAT 批处理脚本教程

    第一章 批处理基础第一节 常用批处理内部命令简介 批处理定义:顾名思义,批处理文件是将一系列命令按一定的顺序集合为一个可执行的文本文件,其扩展名为BAT或者CMD.这些命令统称批处理命令.小知识:可以 ...

  6. Linux高级文本处理命令

    cut 一.cut命令 功能:cut命令可以从一个文本文件/文本流中提取文本列 语法: cut -d '分割字符' -f fields ##用于有特定分割字符 cut -c 字符区间 ##用于排列整齐 ...

  7. php取两个整数的最大公约数算法大全

    php计算两个整数的最大公约数常用算法 <?php//计时,返回秒function microtime_float (){ list( $usec , $sec ) = explode ( &q ...

  8. 因环境变量设置问题引起的command not found

    ls command not found vi command not found 只要的原因是因为环境变量的问题,编辑profile文件没有写正确,导致在命令行下 ls等命令不能够识别.解决办法:在 ...

  9. 既做无线客户端又做无线ap、又可只存在一种模式

    1. 1.1 打开 /barrier_breaker/package/base-files/files/etc/init.d 加入 disable_sta_mode_wifi_interfaces # ...

  10. 搭建openwrt_x86虚拟机环境

    1.下载源码 http://downloads.openwrt.org/snapshots/trunk/x86/openwrt-x86-generic-combined-ext4.img.gz 2.格 ...