我们定义的类的成员函数中,常常有一些成员函数不改变类的数据成员,也就是说,这些函数是"只读"函数,而有一些函数要修改类数据成员的值。如果把不改变数据成员的函数都加上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 的意义的更多相关文章

  1. c++ 在类函数后加const的意义

    我们定义的类的成员函数中,常常有一些成员函数不改变类的数据成员,也就是说,这些函数是"只读"函数,而有一些函数要修改类数据成员的值.如果把不改变数据成员的函数都加上const关键字 ...

  2. C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解

    http://blog.csdn.net/gmstart/article/details/7046140 在C++的类定义里面,可以看到类似下面的定义: 01 class List { 02 priv ...

  3. 关于C++类的成员函数是否要加关键字“const”

    原则:类的成员函数在小括号后大括号前加上 const ,代表不准备改变对象的数据.不加的话代表有可能会改变对象的数据. 1.当常量对象,就是加上const修饰的类的成员去调用常量成员函数时,这表示:对 ...

  4. php function 定义时函数名前加&符号的意义

    看了很多帖子,但是都不能理解,又去看了很多资料,终于名白了.记下备忘. 问题:php在声明函数时,函数名前面的&符号有什么用? 一直想不通.很多帖子说类似于变量的$a=&$b,但是$b ...

  5. 怎么在const成员函数里面调用非const成员函数?

    举个例子: 定义了一个类的const实例,怎么让他也能调用非能调用非const成员函数class foo{public:void test1() {cout << "I am n ...

  6. 类 this指针 const成员函数 std::string isbn() const {return bookNo;}

    转载:http://www.cnblogs.com/little-sjq/p/9fed5450f45316cf35f4b1c17f2f6361.html C++ Primer 第07章 类 7.1.2 ...

  7. C++:类的成员函数定义方式

    1.成员函数的第一种定义方式:在类声明中只给出成员函数的原型,而将成员函数的定义 放在类的外部. 返回值类型 类名::成员函数名(参数表) {      函数体  } class Point{ pub ...

  8. 函数名后加const

    通过把类成员函数声明为const   以表明它们不修改类对象. 任何不会修改数据成员的函数都应该声明为const类型.如果在编写const成员函数时,不慎修改了数据成员,或者调用了其它非const成员 ...

  9. const成员函数可以将非const指针作为返回值吗?

    先给出一段代码 class A { int *x; public: int *f() const { return x; } }; 成员函数f返回指向私有成员 x 的非常量指针,我认为这会修改成员x ...

随机推荐

  1. webpack中使用html-webpack-plugin生成HTML文件并主动插入css和js引入标签

    html-webpack-plugin clean-webpack-plugin 一.html-webpack-plugin 由于打包时生成的css样式文件和js脚本文件会采用hash值作为文件命名的 ...

  2. 实现双向绑定Proxy比defineproperty优劣如何?

    前言 双向绑定其实已经是一个老掉牙的问题了,只要涉及到MVVM框架就不得不谈的知识点,但它毕竟是Vue的三要素之一. Vue三要素 响应式: 例如如何监听数据变化,其中的实现方法就是我们提到的双向绑定 ...

  3. 【Swift后台】环境安装

    macOS 在macOS上使用Vapor,需要Xcode 9.3或更高版本.Swift 4.1或更高版本.安装还需要Homebrew命令. 检查Swift版本: swift --version Vap ...

  4. win10 修改文件夹右击默认打开程序

    1.注册表打开 cmd  regedit 2.打开如下位置 3.编辑图中2个Anycode.command的值 为打开 保存即可

  5. 【2017-04-19】C#中String.Format格式使用

    例子: int a =9; string s= a.ToString("000"); Console.Write(s); 打印出来就是009 C#格式化数值结果表 字符 说明 示例 ...

  6. linux工具之lsof

      1.lsof  ( list    open   files)   lsof(list open files) 是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通 ...

  7. jdk提供的线程协调API suspend/resume wait/notify park/unpark

    线程通信(如 线程执行先后顺序,获取某个线程执行的结果等)有多种方式: 文件共享 线程1 --写入--> 文件 < --读取-- 线程2 网络共享 变量共享 线程1 --写入--> ...

  8. zencart更改css按钮的宽度css buttons

    includes\functions\html_output.php 大概323行的zenCssButton函数 function zenCssButton($image = '', $text, $ ...

  9. Python中关于txt的简单读写模式与操作

    Python中关于txt的简单读写操作 常用的集中读写模式: 1.r 打开只读文件,该文件必须存在. 2.r+ 打开可读写的文件,该文件必须存在. 3.w 打开只写文件,若文件存在则文件长度清为0,即 ...

  10. keil中使用Astyle格式化你的代码的方法-keil4 keil5通用

    简介:在给RTT 提交代码,需要符合RT-Thread 的代码规范,本文简单介绍如何使用Astyle 格式化为符合RTT要求的代码风格. 关于Astyle Astyle 的全称是Artistic St ...