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. 【转】CSS样式覆盖规则

    大家都知道CSS的全称叫做“层叠样式表”,但估计很多人都不知道“层叠”二字的含义.其实,“层叠”指的就是样式的覆盖,当一个元素被运用上多种样式,并且出现重名的样式属性时,浏览器必须从中选择一个属性值, ...

  2. ViewModelBase && ObservableObject

    ViewModelBase && ObservableObject 在Mvvm中,ViewModel和Model都需要具有通知界面更新数据的能力,这都要借助于WPF中的 INotify ...

  3. http缓存提高性能

    秋招也算是正式结束了,现在整理一下笔记,当作巩固一下知识,也希望这个对大家有帮助 http 缓存 和 cdn 缓存可以说是面试必问的问题,竟然是必问的问题,那就总结全面一点- http缓存机制 缓存分 ...

  4. DOM与元素节点内联样式

    获取.设置及移除单个内联 CSS 属性 每个 HTML 元素都有个 style 属性,可以用来插入针对该元素的内联 CSS 属性. <div style='background-color:bl ...

  5. iptables规则组成

    一.四张表五条链 组成部分:四张表 + 5条链(Hook point) + 规则 四张表:filter nat mangle raw 五条链:PREROUTING INPUT FORWARD OUTP ...

  6. .net程序员转行做手游开发经历(三)

    这次就主要讲讲我们开发的过程. 策划是我们团队的一个人成员专门负责,我们几个算是出谋划策.我这边的理解是,策划首先需要对所做的事情一定要有一定的把握,意思是尽可能的想到这件事情的影响范围,类似项目管理 ...

  7. web安全——目录

    说明 写这个目录是为了方便阅读.也是为了记录统一的问题. 这个系列,并不一定是全的,也不一定是对的,所以请大家多做过滤. 这里面场景比较多的是本人在实践中遇到的问题,然后自己思考抽象的. 目录 web ...

  8. HTML5+JS 《五子飞》游戏实现(三)页面和棋盘棋子

    前面两节,我们已经对<五子飞>有个初步的认识,对走棋路线也有了基本的了解,现在里沃特继续跟大家分享HTML页面,另外把棋盘棋子也画出来. 演示地址:http://www.lyout.com ...

  9. UTF-8 's format

    几篇比较好的博客 古腾龙的博客:编码规则(UTF-8 GBK) GBK 千千秀字 shell set man ascii可以查看ascii码表,man utf-8看以查看utf-8的帮助 Unicod ...

  10. rabbitmq 相关方法

    //连接$conn_args = array( 'host'=>'127.0.0.1' , 'port'=> '5672', 'login'=>'guest' , 'password ...