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. Linux&C open creat read write lseek 函数用法总结

    一:五个函数的参数以及返回值. 函数                                 参数                      返回值     open (文件名,打开方式以及读 ...

  2. C++ Qt 项目实战(一)之文本编辑器

    文本编辑器例图 项目开发环境 系统版本:windows10 QT 版本: 5.9.9 开发语言:C++ 已实现功能 文件操作:新建,打开,保存,另存为,打印,退出 编辑操作:复制,粘贴,剪切,查找,替 ...

  3. Fiddler抓包工具简介:(二)下载安装及配置证书和代理

    Fiddler下载安装及配置 一.安装过程: 下载官网:https://www.telerik.com/fiddler 安装过程:一路next即可 启动Fiddler:当你启动了Fiddler,程序将 ...

  4. 手把手从0到1:搭建Kubernetes集群

    搭建 k8s 集群网上很多教程,如果是手工部署或者实验环境可以直接使用 MiniKube 或者 Kind,来在本地启动简单的 Kubernetes 集群进行后面的学习即可.如果是使用 MiniKube ...

  5. PTA 6-1 求采用邻接矩阵作为存储结构的无向图各顶点的度 (6分)

    PTA 6-1 求采用邻接矩阵作为存储结构的无向图各顶点的度 (6分) 函数接口定义: 函数接口为: void dgree(MGraph G); G为采用邻接矩阵作为存储结构的有向图 裁判测试程序样例 ...

  6. [前端随笔][Vue] 多级菜单实现思路——组件嵌套

    说在前面 本篇记录学习了vue-element-admin中的多级菜单的实现 [传送门] @vue/cli 4.2.2:vuex:scss:组件嵌套 正文 创建项目 npm create 项目名 // ...

  7. MySQL基础语句(查询)

    students表 id class_id name gender score 1 1 小明 M 90 2 1 小红 F 95 3 1 小军 M 88 4 1 小米 F 73 5 2 小白 F 81 ...

  8. selenium实战:窗口化爬取*宝数据(附源码链接)

    完整代码&火狐浏览器驱动下载链接:https://pan.baidu.com/s/1pc8HnHNY8BvZLvNOdHwHBw 提取码:4c08 双十一刚过,想着某宝的信息看起来有些少很难做 ...

  9. N皇后问题解法

    // // Created by Administrator on 2021/8/5. // #ifndef C__TEST01_NQUEENS_HPP #define C__TEST01_NQUEE ...

  10. Python实战:截图识别文字,过万使用量版本!(附源码!!)

    前人栽树后人乘凉,以不造轮子为由 使用百度的图片识字功能,实现了一个上万次使用量的脚本. 系统:win10 Python版本:python3.8.6 pycharm版本:pycharm 2021.1. ...