this 指针
来源:互联网 发布:hits算法 编辑:程序博客网 时间:2024/06/05 03:36
两个矩形对象rect1 和rect2 各有自己的m_color 成员变量,但
rect1.setcolor 和rect2.setcolor 却都通往唯一的CRect::setcolor 成员函数。那么
CRect::setcolor 如何处理不同对象中的m_color?答案是:成员函数有一个隐藏参数,名
为this 指针。当你调用:
rect1.setcolor(2); // rect1 是CRect 对象
rect2.setcolor(3); // rect2 是CRect 对象
编译器实际上为你做出来的码是:
CRect::setcolor(2, (CRect*)&rect1);
CRect::setcolor(3, (CRect*)&rect2);
不过,由于CRect 本身并没有声明setcolor,它是从CShape 继承来的,所以编译
器实际上产生的码是:
CShape::setcolor(2, (CRect*)&rect1);
CShape::setcolor(3, (CRect*)&rect2);
多出来的参数,就是所谓的this 指针。至于类别之中,成员函数的定义:
class CShape
{
...
public:
void setcolor(int color) { m_color = color; }
};
被编译器整治过后,其实是:
class CShape
{
...
public:
void setcolor(int color, (CShape*)this) { this->m_color = color; }
};
- this指针
- this指针
- this指针
- this指针
- this指针
- this 指针
- this指针
- this指针----
- this指针
- this 指针
- This 指针
- this指针
- This指针
- this指针
- this指针
- this 指针
- this指针
- this指针
- C/C++函数调用的几种方式(转)
- LayoutInflater
- VB开发ActiveX控件步骤
- shell script范例
- Oracle 表缓存(caching table)的使用
- this 指针
- 千万不要搞IT的十大理由
- 物理引擎Box2D-- 愤怒的小鸟
- 2015年手机应用程序市场预测
- 多种 上传工具
- 注释里的诅咒:哪种语言遭受最多的咒骂?
- POJO 转自百度百科
- 重装windows后easybcd修复ubuntu启动项
- win8的特色真能融入寻常百姓家么?