您的位置:首页 > 编程语言 > C语言/C++

VC++ 的this关键字

2015-06-05 15:02 253 查看
每个成员函数中都隐含包含一个this指针作为函数桉树,并在函数调用时将对象自身的地址隐含作为实际参数传递

</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">#include "stdafx.h"
#include "string.h"

class CBook{
public:
int m_Pages;

/*
每个成员函数中都隐含包含一个this指针作为函数桉树,并在函数调用时将对象自身的
地址隐含作为实际参数传递
这个方法实际相当于:void OutputPages2(CBook* this){}
*/
void OutputPages(){
//printf("%d\n",m_Pages);
printf("%d\n",this->m_Pages); //跟上面一条语句等价
}
};

int _tmain(int argc, _TCHAR* argv[])
{
CBook vbBook,vcBook;
vbBook.m_Pages = 512;
vcBook.m_Pages=570;
vbBook.OutputPages();
vcBook.OutputPages();

return 0;

/*
输出:
512
570
*/
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: