const 成员函数
我们知道,在成员函数中,如果没有修改成员变量,应该给成员函数加上 const 修饰符,例如
#include <iostream> using namespace std; class Foo
{
public:
Foo(int x) : _x(x) {}
int getX() const { return _x; }
private:
int _x;
}; int main()
{
Foo f();
cout << f.getX() << endl;
return ;
}
如果不加 const 修饰符,当使用 const 对象调用成员函数时,编译报错:
#include <iostream> using namespace std; class Foo
{
public:
Foo(int x) : _x(x) {}
int getX() { return _x; }
private:
int _x;
}; int main()
{
const Foo f();
cout << f.getX() << endl;
return ;
}
jingyg@jingyg:~/share/mytest/cpp_test$ g++ -std=c++ test.cpp
test.cpp: In function ?.nt main()?.
test.cpp::: error: passing ?.onst Foo?.as ?.his?.argument of ?.nt Foo::getX()?.discards qualifiers [-fpermissive]
由测试可知:
| const 对象 | 非 const 对象 | |
| const 成员函数 | 成功 | 成功 |
| 非 const 成员函数 | 失败 | 成功 |
const 对象有一个隐藏含义:保证成员变量不变。
const 变量还可以作为函数签名的一部分:
#include <iostream> using namespace std; class Foo
{
public:
Foo(int x) : _x(x) {}
int getX() const { cout << "with const" << endl; return _x; }
int& getX() { cout << "without const" << endl; return _x; }
private:
int _x;
}; int main()
{
const Foo f();
cout << f.getX() << endl; Foo f1();
int& x = f1.getX();
x = ;
cout << f1.getX() << endl;
return ;
}
jingyg@jingyg:~/share/mytest/cpp_test$ g++ -std=c++ test.cpp
jingyg@jingyg:~/share/mytest/cpp_test$ ./a.out
with const without const
without const
可以看到 const 对象调用 const 成员函数,非 const 对象调用非 const 成员函数
const 成员函数的更多相关文章
- const成员函数
尽管函数名和参数列表都相同,void foo( ) const成员函数是可以与void foo( )并存的,可以形成重载! 我们假设调用语句为obj.foo(),如果obj为non-const对象,则 ...
- c++ 学习之const专题之const成员函数
一些成员函数改变对象,一些成员函数不改变对象. 例如: int Point::GetY() { return yVal; } 这个函数被调用时,不改变Point对象,而下面的函数改变Point对象: ...
- 关于 const 成员函数
成员函数如果是const意味着什么? 有两个流行概念:物理常量性和逻辑常量性. C++对常量性的定义采用的是物理常量性概念,即const 成员函数不可以更改对象内任何non-static成员变量.例如 ...
- C++ Const成员函数
一些成员函数改变对象,一些成员函数不改变对象. 例如: int Point::GetY() { return yVal; } 这个函数被调用时,不改变Point对象,而下面的函数改变Point对象 ...
- C++类的const成员函数、默认的构造函数、复制形参调用函数(转)
C++类的const成员函数 double Sales_item::avg_price() const { } const关键字表明这是一个const成员函数,它不可以修改Sales_item类的成员 ...
- c++中的const参数,const变量,const指针,const对象,以及const成员函数
const 是constant 的缩写,“恒定不变”的意思.被const 修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性.所以很多C++程序设计书籍建议:“Use const whe ...
- 拷贝构造函数和const成员函数
实验原因 说明如何使用const描述保护类数据不会意外修改. 编译环境 vc6sp6 + win7x64 工程下载 copyConstruction_constMemberFunction.zip ...
- 12.C++-构造函数与析构函数调用顺序,const成员函数,const对象
单个对象创建时,构造函数的调用顺序 1.首先判断该对象的类是否拥有父类,若有则先调用父类的构造函数 2.判断该对象的成员是否是其它类的成员,若是则调用成员变量的构造函数(调用顺序和声明顺序相同) 3. ...
- Item 16: 让const成员函数做到线程安全
本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 如果我们在数学领域里工作,我们可能会发现用一个类来表示多项式会很方 ...
随机推荐
- Thymeleaf的超链接与AJAX的跳转问题
//th:href :超链接<a th:href="@{/list}"></a>//可以在其他页面跳转yt <form id="msform ...
- python 条件分支与循环
一.if判断: 语法一: if 条件: # 条件成立时执行的子代码块 代码1 代码2 代码3 示例: sex='female' age=18 is_beautiful=True if sex == ' ...
- EventBus 线程切换原理
主要问题其实只有两个,其一:如何判断当前发送事件的线程是否是主线程:其二:如何在接收事件时指定线程并执行: 一个一个来看. 1.如何判断是否在主线程发送 EventBus在初始化的时候会初始化一个Ma ...
- Ubuntu16.04安装Redis并配置
Ubuntu16.04安装Redis并配置 2018年05月22日 10:40:35 Hello_刘 阅读数:29146 Ubuntu16.04安装Redis并配置 1):安装: 1:终端命令下载 ...
- Linux shell if判断语句
无论什么编程语言都离不开条件判断.SHELL也不例外. 大体的格式如下: if list then do something here elif list then do another thing ...
- stop()在animate中的用法
stop 是jQuery中用于控制页面动画效果的方法.运行之后立刻结束当前页面上的动画效果.stop在新版jQuery中添加了2个参数:第一个参数的意思是是否清空动画序列,也就是stop的是当前元素的 ...
- 洛谷P4178 Tree (算竞进阶习题)
点分治 还是一道点分治,和前面那道题不同的是求所有距离小于等于k的点对. 如果只是等于k,我们可以把重心的每个子树分开处理,统计之后再合并,这样可以避免答案重复(也就是再同一个子树中出现路径之和为k的 ...
- MT【321】分类线性规划
若二次函数$f(x)=ax^2+bx+c(a,b,c>0)$有零点,则$\min\{\dfrac{b+c}{a},\dfrac{c+a}{b},\dfrac{a+b}{c}\}$ 的最大值为__ ...
- linux device drivers ch02
ch02.构造和运行模块 模块的构造: #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(&qu ...
- Tomcat设计模式
omcat 系统架构与设计模式,第 2 部分 设计模式分析 系列内容: 此内容是该系列 2 部分中的第 2 部分: Tomcat 系统架构与设计模式 门面设计模式 门面设计模式在 Tomcat 中有多 ...