Extraction from C++ primer 5th Edition 7.1.2

The purpose of the const that follows the parameter list of an ordinary member function is to modify the type of the implicit this pointer.

By default, the type of this is a const pointer to the nonconst version of the class type. For example, by default, the type of this in a member function f of class T is T *const. Although this is implicit, it follows the normal initialization rules, which means that (by default) we cannot bind this to a const object. This fact, in turn, means that we cannot call an ordinary member function on a const object.

If f were an ordinary function and if this were an ordinary pointer parameter, we would declare this as const T *const. After all, the body of f does not change the object to which this points, so our function would be more flexible if this were a pointer to const.

However, this is implicit and does not appear in the parameter list. There is no place to indicate that this should be a pointer to const. The language resolves this problem by letting us put const after the parameter list of a member function. A const follow the parameter list indicates that this is a pointer to const. Member functions that use const in this way are const member functions.

Objects that are const, and references or pointers to const objects, may call only const member functions.

C++ 之const Member Functions的更多相关文章

  1. C++ Member Functions的各种调用方式

    [1]Nonstatic Member Functions(非静态成员函数) C++的设计准则之一就是:nonstatic member function至少必须和一般的nonmember funct ...

  2. Some interesting facts about static member functions in C++

    Ref http://www.geeksforgeeks.org/some-interesting-facts-about-static-member-functions-in-c/ 1) stati ...

  3. C++:Special Member Functions

    Special Member Functions 区别于定义类的行为的普通成员函数,类内有一类特殊的成员函数,它们负责类的构造.拷贝.移动.销毁. 构造函数 构造函数控制对象的初始化过程,具体来说,就 ...

  4. [C++] static member variable and static const member variable

    static member variable[可读可写] 可以通过指针间接修改变量的值 static const member variable[只读] 压根就不可以修改变量的值,会报错

  5. virtual member functions(单一继承情况)

    virtual member functions的实现(就单一继承而言): 1.实现:首先会给有多态的class object身上增加两个members:一个字符串或数字便是class的类型,一个是指 ...

  6. Use of ‘const’ in Functions Return Values

    Use of 'Const' in Function Return Values 为什么要在函数的返回值类型中添加Const? 1.Features Of the possible combinati ...

  7. warning 4510 with const member in struct

    I write a code section as this struct My{const int a;}; OK, then set the warning level then I will g ...

  8. C++进阶--const和函数(const and functions)

    // const和函数一起使用的情况 class Dog { int age; string name; public: Dog() { age = 3; name = "dummy&quo ...

  9. 2_成员函数(Member Functions)

    成员函数以定从属于类,不能独立存在,这是它与普通函数的重要区别.所以我们在类定义体外定义成员函数的时候,必须在函数名之前冠以类名,如Date::isLeapYear().但如果在类定义体内定义成员函数 ...

随机推荐

  1. 2016年优秀的java网站分享

    java中文网站 伯乐在线java版:http://www.importnew.com/ 码农网:http://www.codeceo.com/ infoq:http://www.infoq.com/ ...

  2. 项目规范性检测工具Lint

    项目规范性检测工具lint.bat 一.Lint基本概念介绍 Android Lint是SDK Tools 16 (ADT 16)之后才引入的工具,通过它对Android工程源代码进行扫描和检查,可发 ...

  3. [BZOJ3714][PA2014]Kuglarz(MST)

    题目: Description 魔术师的桌子上有n个杯子排成一行,编号为1,2,…,n,其中某些杯子底下藏有一个小球,如果你准确地猜出是哪些杯子,你就可以获得奖品.花费c_ij元,魔术师就会告诉你杯子 ...

  4. matlab画图形函数 semilogx

    matlab画图形函数 semilogx loglog 主要是学习semilogx函数,其中常用的是semilogy函数,即后标为x的是在x轴取对数,为y的是y轴坐标取对数.loglog是x y轴都取 ...

  5. java文件cmd运行出现中文乱码

    今天刚开始学java,使用cmd命令执行java文件的时候,发现中文打出来是一串乱码. 于是就百度了一下,发现一个行之有效的方法. 首先使用命令:javac -encoding utf-8 Hello ...

  6. swfupload提示“错误302”的解决方法

    1.关于图片上传控件,flash控件的显示效果要好一些,本人使用swfupload 2.swfupload上传控件使用方式详见文档 http://www.leeon.me/upload/other/s ...

  7. 切割haproxy的日志

    日志的切割有以下几种方法: 1.写个定时任务,每天某个时间点把旧的日志重命名,并对服务重启使其重新打开日志并写入. 2.通过管道的方式把新产生的日志写到另外一个日志文件里. 3.通过logrotate ...

  8. jenkins 把包传到远程服务器上

    首先我们在 一台服务器上部署svn,maven,jdk,tomcat,nexus,还有Jenkins. 这里我主要记录Jenkins. 首先我们从网上下载Jenkins的包 wget http://m ...

  9. [转]Null value was assigned to a property of primitive type setter of"原因及解决方法

    原文地址:http://blog.csdn.net/kevinzhangfei/article/details/6995316 在action请求数据的过程中报出"Null value wa ...

  10. [转]Hibernate update和saveOrUpdate详解

    原文地址:http://www.iteye.com/topic/2712 先来点概念: 在Hibernate中,最核心的概念就是对PO的状态管理.一个PO有三种状态: 1.未被持久化的VO 此时就是一 ...