成员函数定义后面加 const 的意义
我们定义的类的成员函数中,常常有一些成员函数不改变类的数据成员,也就是说,这些函数是"只读"函数,而有一些函数要修改类数据成员的值。如果把不改变数据成员的函数都加上const关键字进行标识,显然,可提高程序的可读性。
其实,它还能提高程序的可靠性,已定义成const的成员函数,一旦企图修改数据成员的值,则编译器按错误处理。
c++ 函数前面和后面 使用const 的作用:
前面使用const 表示返回值为const
后面加 const表示函数不可以修改class的成员 (仅为可读,不可写入)
请看这两个函数
const int getValue();
int getValue2() const;
头文件定义
/*
* FunctionConst.h
*/ #ifndef FUNCTIONCONST_H_
#define FUNCTIONCONST_H_ class FunctionConst {
public:
int value;
FunctionConst();
virtual ~FunctionConst();
const int getValue();
int getValue2() const;
}; #endif /* FUNCTIONCONST_H_ */
源文件实现
/*
* FunctionConst.cpp
*/ #include "FunctionConst.h" FunctionConst::FunctionConst():value() {
// TODO Auto-generated constructor stub } FunctionConst::~FunctionConst() {
// TODO Auto-generated destructor stub
} const int FunctionConst::getValue(){
return value;//返回值是 const, 使用指针时很有用.
} int FunctionConst::getValue2() const{
//此函数不能修改class FunctionConst的成员属性 value
value = ;//错误的, 因为函数后面加 const
return value;
}
参考 https://stackoverflow.com/questions/2157458/using-const-in-classs-functions
what is the real definition and use of const ?
A const method can be called on a const object:
class CL2
{
public:
void const_method() const;
void method(); private:
int x;
}; const CL2 co;
CL2 o; co.const_method(); // legal
co.method(); // illegal, can't call regular method on const object
o.const_method(); // legal, can call const method on a regulard object
o.method(); // legal
Furthermore, it also tells the compiler that the const method should not be changing the state of the object and will catch those problems:
void CL2::const_method() const
{
x = ; // illegal, can't modify a member in a const object
}
There is an exception to the above rule by using the mutable modifier, but you should first get good at const correctness before you venture into that territory.
成员函数定义后面加 const 的意义的更多相关文章
- c++ 在类函数后加const的意义
我们定义的类的成员函数中,常常有一些成员函数不改变类的数据成员,也就是说,这些函数是"只读"函数,而有一些函数要修改类数据成员的值.如果把不改变数据成员的函数都加上const关键字 ...
- C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解
http://blog.csdn.net/gmstart/article/details/7046140 在C++的类定义里面,可以看到类似下面的定义: 01 class List { 02 priv ...
- 关于C++类的成员函数是否要加关键字“const”
原则:类的成员函数在小括号后大括号前加上 const ,代表不准备改变对象的数据.不加的话代表有可能会改变对象的数据. 1.当常量对象,就是加上const修饰的类的成员去调用常量成员函数时,这表示:对 ...
- php function 定义时函数名前加&符号的意义
看了很多帖子,但是都不能理解,又去看了很多资料,终于名白了.记下备忘. 问题:php在声明函数时,函数名前面的&符号有什么用? 一直想不通.很多帖子说类似于变量的$a=&$b,但是$b ...
- 怎么在const成员函数里面调用非const成员函数?
举个例子: 定义了一个类的const实例,怎么让他也能调用非能调用非const成员函数class foo{public:void test1() {cout << "I am n ...
- 类 this指针 const成员函数 std::string isbn() const {return bookNo;}
转载:http://www.cnblogs.com/little-sjq/p/9fed5450f45316cf35f4b1c17f2f6361.html C++ Primer 第07章 类 7.1.2 ...
- C++:类的成员函数定义方式
1.成员函数的第一种定义方式:在类声明中只给出成员函数的原型,而将成员函数的定义 放在类的外部. 返回值类型 类名::成员函数名(参数表) { 函数体 } class Point{ pub ...
- 函数名后加const
通过把类成员函数声明为const 以表明它们不修改类对象. 任何不会修改数据成员的函数都应该声明为const类型.如果在编写const成员函数时,不慎修改了数据成员,或者调用了其它非const成员 ...
- const成员函数可以将非const指针作为返回值吗?
先给出一段代码 class A { int *x; public: int *f() const { return x; } }; 成员函数f返回指向私有成员 x 的非常量指针,我认为这会修改成员x ...
随机推荐
- WPF跨线程操作UI界面控件
在WPF应用中,如果遇到多线程的需求时,如果引用WPF控件时会引发异常,异常内容:调用线程无法访问此对象,因为另一个线程拥有该对象.具体如下: 调用代码: ThreadcountThread= ...
- 使用flex布局解决百分比高度元素垂直居中
方法一: align-self(解决父元素下面单个子元素布局方式) 父级加上 div{display:flex} 子元素 span { flex-grow: 1; align-self: center ...
- javascript的特点这些
javascript的特点(1)用于解释性执行的脚本语言.与其他脚本语言一样,JavaScript也是一种解释性语言,它提供了非常方便的开发过程.JavaScript的基本语法结构与C.C++.Jav ...
- 使用原生node.js搭建HTTP服务器,支持MP4视频、图片传输,支持下载rar文件
前言 如何安装node.js,如何搭建一个简易的http服务器我这里就不再赘述了,不懂的同学可以先去学习一下.当然了,我写的也就属于简易版的增强版,大家有什么高见的欢迎提出,然后进入正题. 目录结构 ...
- 巧用Map缓存提升"翻译"速度
在业务编码中,很多情况都需要用到code2Name或者id2Name之间的"翻译",在我的过往经历中发现不少开发人员都是直接双重循环实现这种"翻译".如果一次& ...
- 正确理解这四个重要且容易混乱的知识点:异步,同步,阻塞,非阻塞,5种IO模型
本文讨论的背景是Linux环境下的network IO,同步IO和异步IO,阻塞IO和非阻塞IO分别是什么 概念说明 在进行解释之前,首先要说明几个概念: - 用户空间和内核空间 - 进程切换 - 进 ...
- Active Directory participation features and security extensions
Participation in the Active Directory Samba 3.0 series, as well as the OS since Windows 2000, is pos ...
- Nginx返回大长度的JSON数据被截断
1 添加Nginx参数,增加缓存字符串大小 head{ proxy_buffers 16 512k; //此处值代表nginx 设置 16个 512k 的块进行缓存,总共大小为16*512k prox ...
- celery:强大的定时任务模块
什么是celery 还是一个老生常谈的话题,假设用户注册,首先注册信息入库,然后要调用验证码服务接口,然后根据手机号发送验证码,最后再返回响应给浏览器.但显然调用接口.发送验证码之后成功再给浏览器响应 ...
- SQL练习汇总
--1.选择部门30中的所有员工. --2.列出所有办事员(CLERK)的姓名,编号和部门编号. select ename,empno,deptno from emp where job='CLERK ...