Type of 'this' pointer in C++
In C++, this pointer is passed as a hidden argument to all non-static member function calls. The type of this depends upon function declaration. If the member function of a class X is declared const, the type of this is const X* (see code 1 below), if the member function is declared volatile, the type of this is volatile X* (see code 2 below), and if the member function is declared const volatile, the type of this is const volatile X* (see code 3 below).
Code 1
- 1 #include<iostream>
- 2 class X
- 3 {
- 4 void fun() const
- 5 {
- 6 // this is passed as hidden argument to fun().
- 7 // Type of this is const X*
- 8 }
- 9 };
Code 2
- 1 #include<iostream>
- 2 class X
- 3 {
- 4 void fun() volatile
- 5 {
- 6 // this is passed as hidden argument to fun().
- 7 // Type of this is volatile X*
- 8 }
- 9 };
Code 3
- 1 #include<iostream>
- 2 class X
- 3 {
- 4 void fun() const volatile
- 5 {
- 6 // this is passed as hidden argument to fun().
- 7 // Type of this is const volatile X*
- 8 }
- 9 };
补充:
(1)In an ordinary nonconst member function, the type of this is a const pointer to the class type. We may change the value to which this points but cannot change the address that this holds. In a const member function, the type of this is a const pointer to a const class - type object. We may change neither the object to which this points nor the address that this holds.
(2)What is the purpose of a volatile member function in C++?【from stackoverflow】
References:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-26 09:32:58
Type of 'this' pointer in C++的更多相关文章
- A Tour of Go Methods with pointer receivers
Methods can be associated with a named type or a pointer to a named type. We just saw two Abs method ...
- differences between null pointer and void pointer.
These are two different concepts, you cannot compare them. What the difference between the skunk and ...
- A pointer is a variable whose value is the address of another variable 指针 null pointer 空指针 内存地址0 空指针检验
小结: 1.指针的实际值为代表内存地址的16进制数: 2.不同指针的区别是他们指向的变量.常量的类型: https://www.tutorialspoint.com/cprogramming/c_po ...
- Smart Pointer Guidelines
For Developers > Smart Pointer Guidelines What are smart pointers? Smart pointers are a specif ...
- 'this' pointer in C++
The 'this' pointer is passed as a hidden argument to all nonstatic member function calls and is avai ...
- Html 制作相册
本文主要讲述采用Html5+jQuery+CSS 制作相册的小小记录. 主要功能点: Html5进行布局 调用jQuery(借用官网的一句话:The Write Less, Do More)极大的简化 ...
- 编译器开发系列--Ocelot语言5.表达式的有效性检查
本篇将对"1=3""&5"这样无法求值的不正确的表达式进行检查. 将检查如下这些问题.●为无法赋值的表达式赋值(例:1 = 2 + 2)●使用非法的函数 ...
- YYModel 源码解读(二)之NSObject+YYModel.h (1)
本篇文章主要介绍 _YYModelPropertyMeta 前边的内容 首先先解释一下前边的辅助函数和枚举变量,在写一个功能的时候,这些辅助的东西可能不是一开始就能想出来的,应该是在后续的编码过程中 ...
- CSS3与页面布局学习总结(二)——Box Model、边距折叠、内联与块标签、CSSReset
一.盒子模型(Box Model) 盒子模型也有人称为框模型,HTML中的多数元素都会在浏览器中生成一个矩形的区域,每个区域包含四个组成部分,从外向内依次是:外边距(Margin).边框(Border ...
随机推荐
- 【Jenkins】jenkins构建python项目提示:'python' 不是内部或外部命令,也不是可运行的程序或批处理文件
一.问题:jenkins构建python项目提示:'python' 不是内部或外部命令,也不是可运行的程序或批处理文件 二.原因:要在jenkins配置本地环境变量 三.解决方案:添加python.e ...
- 工程中实际问题解决两例——基于C#
工程中实际问题解决两例--基于C# 目录 工程中实际问题解决两例--基于C# 1. try catch的妙用 1.1 遇到问题的现象 1.2 原因分析 1.2.1 先从数据库入手 1.2.2 代码日志 ...
- PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilder,阿里MAVEN仓库地址更新为了https问题
http://maven.aliyun.com/nexus/content/groups/public/,仓库地址更新为了https,所以下载时需要ssl认证,我们可以忽略ssl检查导致的问题,我们可 ...
- NFS导致df -h卡主解决
NFS导致df -h无法使用解决 NFS服务意外断开,导致挂载的客户端"df -Th"命令无法使用,及挂载目录无法"cd""ls"解决思路: ...
- [gym102832J]Abstract Painting
考虑每一个圆即对应于区间$[x_{i}-r_{i},x_{i}+r_{i}]$,可以看作对于每一个区间,要求所有右端点严格比其小的区间不严格包含左端点 用$f_{i}$表示仅考虑右端点不超过$i$的区 ...
- 小程序嵌套H5的方式和技巧(一)
文章内多次使用了关键字"壳",首先先解释一下什么是壳 壳: 小程序由原生的web-view组件形成的页面,页面只包含技术逻辑(如打开H5页面),不包含具体业务接口请求和业务逻辑处理 ...
- 7.4 k8s结合ceph rbd、cephfs实现数据的持久化和共享
1.在ceph集群中创建rbd存储池.镜像及普通用户 1.1.存储池接镜像配置 创建存储池 root@u20-deploy:~# ceph osd pool create rbd-test-pool1 ...
- vue简单语法梳理
小图不够清楚,可以点击大图查看.
- Codeforces 1089I - Interval-Free Permutations(析合树计数)
Codeforces 题面传送门 & 洛谷题面传送门 首先题目中涉及排列的 interval,因此可以想到析合树.由于本蒟蒻太菜了以至于没有听过这种神仙黑科技,因此简单介绍一下这种数据结构:我 ...
- CURL常用参数
1. CURL简介 cURL是一个利用URL语法在命令行下工作的文件传输工具.它支持文件上传和下载,是综合传输工具.cURL就是客户端(client)的URL工具的意思. 2. 常用参数 -k:不校验 ...