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().但如果在类定义体内定义成员函数 ...
随机推荐
- 利用performance属性查看网页性能
一般我们可以通过浏览器的调试工具-网络面板,或者代理工具查看网页加载过程中的各个阶段的耗时.而利用window.performance属性则可以获得更为精确的原始数据,以毫秒为单位,精确到微秒. pe ...
- 直流调速系统Modelica基本模型
为了便于在OpenModelica进行仿真,形成一个完整的仿真模型,没有使用第三方的库,参照了DrModelica的例程,按照Modelica库的开源模型定义了所用的基本元件模型. 首先给出一些基本类 ...
- DataTrigger 绑定枚举
在触发器中绑定枚举类型: <ControlTemplate.Triggers> <DataTrigger Binding="{Binding CheckStateEnum} ...
- STM32-外部中断,没有硬件干扰就是快乐
一:触发方式 STM32 的外部中断是通过边沿来触发的,不支持电平触发: 二:外部中断分组 STM32 的每一个GPIO都能配置成一个外部中断触发源,STM32 通过根据引脚的序号不同将众多中断触发源 ...
- 使用pngquant来压缩png资源缩小apk
最近发现了一个叫做pngquant的工具,可以有效的压缩资源文件中的png文件,从而减小发布的apk的大小.我发现这个工具有两个特点: 1. 真无损,压缩后重新运行了我的app发现是没有任何区别的 2 ...
- SQLite剖析之体系结构
1.通过官方的SQLite架构文档,理清大体的系统层次:Architecture of SQLite 2.阅读SQLite Documentation中Technical/Design Documen ...
- Log4net使用(三)
第一步 public class logger { private static ILog Info; private static ILog Error; private static ILog W ...
- koala不支持中文的解决办法(问题出现在使用中文字体时报错)
C:\Program Files\Koala\rubygems\gems\sass-3.4.9\lib\sass 这是我的koala的安装路径,在sass文件夹下打开engine.rb(文本文档打开即 ...
- Windows去除快捷箭头
美化桌面 bat代码: reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shel ...
- Oracle空串与null的处理
来源于:http://blog.itpub.net/24870090/viewspace-1057853/ Oracle空串与null的处理[@more@] Oracle中的空字符串基本上是被当成空N ...