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++的更多相关文章

  1. 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 ...

  2. differences between null pointer and void pointer.

    These are two different concepts, you cannot compare them. What the difference between the skunk and ...

  3. 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 ...

  4. Smart Pointer Guidelines

    For Developers‎ > ‎ Smart Pointer Guidelines What are smart pointers? Smart pointers are a specif ...

  5. 'this' pointer in C++

    The 'this' pointer is passed as a hidden argument to all nonstatic member function calls and is avai ...

  6. Html 制作相册

    本文主要讲述采用Html5+jQuery+CSS 制作相册的小小记录. 主要功能点: Html5进行布局 调用jQuery(借用官网的一句话:The Write Less, Do More)极大的简化 ...

  7. 编译器开发系列--Ocelot语言5.表达式的有效性检查

    本篇将对"1=3""&5"这样无法求值的不正确的表达式进行检查. 将检查如下这些问题.●为无法赋值的表达式赋值(例:1 = 2 + 2)●使用非法的函数 ...

  8. YYModel 源码解读(二)之NSObject+YYModel.h (1)

    本篇文章主要介绍 _YYModelPropertyMeta 前边的内容 首先先解释一下前边的辅助函数和枚举变量,在写一个功能的时候,这些辅助的东西可能不是一开始就能想出来的,应该是在后续的编码过程中 ...

  9. CSS3与页面布局学习总结(二)——Box Model、边距折叠、内联与块标签、CSSReset

    一.盒子模型(Box Model) 盒子模型也有人称为框模型,HTML中的多数元素都会在浏览器中生成一个矩形的区域,每个区域包含四个组成部分,从外向内依次是:外边距(Margin).边框(Border ...

随机推荐

  1. 解决SpringBoot项目部署到服务器后访问Tomcat后404,无法访问Controller

  2. Flink 实践教程:入门(6):读取 PG 数据写入 ClickHouse

    作者:腾讯云流计算 Oceanus 团队 流计算 Oceanus 简介 流计算 Oceanus 是大数据产品生态体系的实时化分析利器,是基于 Apache Flink 构建的具备一站开发.无缝连接.亚 ...

  3. Elasticsearch写入数据的过程是什么样的?以及是如何快速更新索引数据的?

    前言 最近面试过程中遇到问Elasticsearch的问题不少,这次总结一下,然后顺便也了解一下Elasticsearch内部是一个什么样的结构,毕竟总不能就只了解个倒排索引吧.本文标题就是我遇到过的 ...

  4. CTF入门学习3->Web通信基础

    Web安全基础 01 Web通信 这个部分重点介绍浏览器与Web服务器的详细通信过程. 01-00 URL协议 只要上网访问服务器,就离不开URL. URL是什么? URL就是我们在浏览器里输入的站点 ...

  5. [hdu6974]Destinations

    注意到一个人的三条链一定不会同时选(忽略仅选一个终点的限制),因为其有公共点(起点) 换言之,问题相当于给定$3m$条链,选择$m$条没有公共点的链,并最小化代价和 进一步的,显然也不存在多于$m$条 ...

  6. Furion分表分库我也要happy coding

    Furion分表分库集成ShardingCore ShardingCore ShardingCore 易用.简单.高性能.普适性,是一款扩展针对efcore生态下的分表分库的扩展解决方案,支持efco ...

  7. 论文翻译:2020_A Recursive Network with Dynamic Attention for Monaural Speech Enhancement

    论文地址:基于动态注意的递归网络单耳语音增强 论文代码:https://github.com/Andong-Li-speech/DARCN 引用格式:Li, A., Zheng, C., Fan, C ...

  8. HDU 6984 - Tree Planting(数据分治+状压 dp)

    题面传送门 傻逼卡常屑题/bs/bs,大概现场过得人比较少的原因就是它比较卡常罢(Fog 首先对于这样的题我们很难直接维护,不过注意到这个 \(n=300\) 给得很灵性,\(k\) 比较小和 \(k ...

  9. Atcoder Regular Contest 096 D - Sweet Alchemy(贪心+多重背包)

    洛谷题面传送门 & Atcoder 题面传送门 由于再过 1h 就是 NOI 笔试了所以题解写得会略有点简略. 考虑差分,记 \(b_i=c_i-c_{fa_i}\),那么根据题意有 \(b_ ...

  10. Hosts文件详解

    一.缘由 关于我们工作室项目配置过程中,有一个重要但却容易被忽略的环节 - hosts文件的修改. 之前配置hosts文件的时候,只知道那么做就可以了,但并不知道其中的原因,由于开发任务的急迫,也就未 ...