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. Spring MVC 前后端 Json 方式交互和处理

    众所周知,在mvc中,数据是在各个层次之间进行流转是一个不争的事实. 而这种流转,也就会面临一些困境,这些困境,是由于数据在不同世界中的表现形式不同而造成的.   数据在页面上是一个扁平的,不带数据类 ...

  2. Laravel如何优雅的使用Swoole

    背景 正在做一个智能家居的项目(钱低的吓死人怎么办),接收下位机(就是控制智能家居硬件模块的HUB)协议解析,Web端维护硬件状态,利用APP交互.由于下位机数据是发送到服务器的XXX端口,所以必须对 ...

  3. 理解AngularJS生命周期:利用ng-repeat动态解析自定义directive

    ng-repeat是AngularJS中一个非常重要和有意思的directive,常见的用法之一是将某种自定义directive和ng-repeat一起使用,循环地来渲染开发者所需要的组件.比如现在有 ...

  4. webpack 教程资源收集

    学习的过程中收藏了这些优秀教程和的项目,希望对你有帮助. github地址, 有不错的就更新 官方文档 中文指南  初级教程 webpack-howto 作者:Pete Hunt Webpack 入门 ...

  5. 数据库高可用架构(MySQL、Oracle、MongoDB、Redis)

    一.MySQL MySQL小型高可用架构 方案:MySQL双主.主从 + Keepalived主从自动切换   服务器资源:两台PC Server 优点:架构简单,节省资源 缺点:无法线性扩展,主从失 ...

  6. 1019在winddow上面安装MYSQL服务

    -- 在WINDOWS上安装MYSQL,利用运行包直接安装-- 第一步复制文件拷贝到对应目录-- 第二步修改配置文件,创建DATA目录[client]port=3312 [mysql]default- ...

  7. js日期显示效果

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  8. LINUX 配置SVN

    1. 安装SVN  yum -y install subversion 2. 创建版本库目录  mkdir /root/svn/ svnserve -d -r /root/svn/ 3. 创建版本库 ...

  9. poj1144 求不同割点的个数

    Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11914   Accepted: 5519 Descript ...

  10. JavaScript结构三层——思想快速入门

      本文版权归博客园和作者吴双本人所有,转载和爬虫请注明原文地址 http://www.cnblogs.com/tdws/,我是博客园蜗牛,我们共同进步. 今天讨论的是什么 如果你的工作中需要写Jav ...