博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Type of 'this' pointer in C++
阅读量:5307 次
发布时间:2019-06-14

本文共 1741 字,大约阅读时间需要 5 分钟。

 

  

  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
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
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
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:

 

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

      

  转载请注明:  

  2013-11-26  09:32:58

转载于:https://www.cnblogs.com/iloveyouforever/p/3442675.html

你可能感兴趣的文章
Groovy中String转换Gstring用于动态插值
查看>>
查看dmesg,会打出很多的日志“TCP: too many of orphaned sockets”
查看>>
Oracle
查看>>
IT行业始接触
查看>>
Python基础知识点小结
查看>>
MVC利用Routing实现多域名绑定一个站点、二级域名以及二级域名注册Area
查看>>
shell脚本快速入门
查看>>
arch初步美化及各种问题
查看>>
了解SQL Server2005新架构规则的优势
查看>>
HDU 1063 Exponentiation
查看>>
pci空间配置
查看>>
Activity的启动模式
查看>>
on duplicate key update
查看>>
CSS设置字体为楷体
查看>>
nginx配置反向代理解决前后端分离跨域问题
查看>>
SP2 网络故障解决
查看>>
VC中pragma指令简介(转)
查看>>
用 Jenkins + .netcore 2.0 构建
查看>>
POJ 2546
查看>>
NAT的三种方式
查看>>