C++ 之const Member Functions
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的更多相关文章
- C++ Member Functions的各种调用方式
[1]Nonstatic Member Functions(非静态成员函数) C++的设计准则之一就是:nonstatic member function至少必须和一般的nonmember funct ...
- 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 ...
- C++:Special Member Functions
Special Member Functions 区别于定义类的行为的普通成员函数,类内有一类特殊的成员函数,它们负责类的构造.拷贝.移动.销毁. 构造函数 构造函数控制对象的初始化过程,具体来说,就 ...
- [C++] static member variable and static const member variable
static member variable[可读可写] 可以通过指针间接修改变量的值 static const member variable[只读] 压根就不可以修改变量的值,会报错
- virtual member functions(单一继承情况)
virtual member functions的实现(就单一继承而言): 1.实现:首先会给有多态的class object身上增加两个members:一个字符串或数字便是class的类型,一个是指 ...
- Use of ‘const’ in Functions Return Values
Use of 'Const' in Function Return Values 为什么要在函数的返回值类型中添加Const? 1.Features Of the possible combinati ...
- 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 ...
- C++进阶--const和函数(const and functions)
// const和函数一起使用的情况 class Dog { int age; string name; public: Dog() { age = 3; name = "dummy&quo ...
- 2_成员函数(Member Functions)
成员函数以定从属于类,不能独立存在,这是它与普通函数的重要区别.所以我们在类定义体外定义成员函数的时候,必须在函数名之前冠以类名,如Date::isLeapYear().但如果在类定义体内定义成员函数 ...
随机推荐
- Qt 5.2 Creator 和 vs2012 QT 插件的安装
在QT官网下载QT http://qt-project.org/downloads 我下的是64位版本Qt 5.2.1 for Windows 64-bit vs2012插件是 Visual Stu ...
- 【语言基础】c++ 备忘录
1. C++ 整数类型范围 可以参照头文件limits.h定义的宏 #define INT_MAX 2147483647(32bit, 最大10位十进制) #define UINT_MAX ...
- WPF 3D模型 3D场景
1.首先得说明的是这并不是真正的3D,模型被导出为一系列的单个图片,例如一个3D户型图,以某个视角旋转360°,渲染出一系列连续的单个图片文件. 2.在Image.MouseMove事件中添加相应代码 ...
- suggest插件实现下拉选择筛选
实现的效果展示: 代码如下: 1.需要引入jquery,bootstrap-suggest.js,bootstrap.min.css 2.html页面代码: <!DOCTYPE html> ...
- CSS3——让最后一行显示省略号
代码如下: <!DOCTYPE html> <html> <head> <title></title> <meta charset=& ...
- background-size对background-position的影响
CSS3中提出了background-size属性,该属性可以设置背景图片的大小,该属性的值设置为绝对数值或者百分比时对background-position没有任何影响,当设置为contain/co ...
- 关于提高python程序执行效率的思路和想法
相比编译型语言(C系列)python胜在简介的语法和优雅的动态编程体验,但是在执行效率上,python有解释性语言先天的劣势——执行效率较低,为了让编写出的程序获得更快的执行效率,开启此文章. pyt ...
- nodejs实现的简单接口
var http = require('http'); var mysql = require('mysql'); var connection = mysql.createConnection({ ...
- android 颜色对照
<table><tbody> <tr> <td bgcolor="#ffffff" width="30" height ...
- Beta版本冲刺Day2
会议讨论 628: 和建平同学一起合作解决了如何获取勾选事件,将勾选的课程信息存入select表格中.但是当运行更新后的项目遇到了无法连接数据库的问题了,目前还在解决中. 因为碰到了这样的 ...