This 指针

      如果你期望衍生类别重新定义一个成员函数,那么你应该在基础类别中把此函数设为 virtual。
      以单一指令唤起不同函数,这种性质称为Polymorphism,意思是"the ability toassume many forms",也就是多态。
      虚拟函数是C++ 语言的Polymorphism 性质以及动态绑定的关键。


class CShape   // 形状  
02. {  
03.    private:  
04.      int m_color;  
05.    public:  
06.      void setcolor(int color) { m_color = color; }  
07. };  
08.  
09.  classCRect : public CShape    // 矩形是一种形状,它会继承 m_color 和setcolor()  
10. {      
11.    public:  
12.      voiddisplay() { ... }  
13. };  
14.  
15. class CEllipse : public CShape   // 椭圆形是一种形状,它会继承 m_color 和setcolor()  
16. {       
17.    public:  
18.      voiddisplay() { ... }  
19. };  
20.  
21. class CTriangle : public CShape  // 三角形是一种形状,它会继承 m_color 和setcolor()  
22. {     public:  
23.       void display() { ... }  
24. };  
25.  
26. classCSquare : public CRect    // 四方形是一种矩形  
27. {  
28.     public:  
29.       void display() { ... }  
30. };  
31.  
32. class CCircle : public CEllipse  // 圆形是一种椭圆形  
33. {  
34.     public:  
35.       void display() { ... }  
36. };  
37.    //于是可以这么动作:  
38.    CSquare square;  
39.    CRect rect1, rect2;  
40.    CCircle circle;  
41.    square.setcolor(1); // 令 square.m_color = 1;  
42.    m_color m_color  
43.    CRect::setcolor(int color,  
44.    square.display(); // 调用CSquare::display  
45.    rect1.setcolor(2); // 于是rect1.m_color = 2  
46.    rect1.display(); // 调用CRect::display  
47.    rect2.setcolor(3); // 于是rect2.m_color = 3  
48.    rect2.display(); // 调用CRect::display  
49.    circle.setcolor(4); // 于是circle.m_color = 4  
50.    circle.display(); // 调用CCircle::display  

       两个矩形对象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);    
02.CRect::setcolor(3, (CRect*)&rect2);  
    不过,由于CRect 本身并没有声明setcolor,它是从CShape 继承来的,所以编译器实际上产生的码是:
CShape::setcolor(2, (CRect*)&rect1);    
02.CShape::setcolor(3, (CRect*)&rect2);  
    多出来的参数,就是所谓的this 指针。至于类别之中,成员函数的定义:
class CShape    
02.  
03....    
04.public:    
05.void setcolor(int color) { m_color = color; }   
    被编译器整治过后,其实是:
class CShape  
02.{ ...  
03.  public:  
04.  void setcolor(int color, (CShape*)this) { this->m_color = color; }  
05.};  




### 作用 `this` 指针是 C++ 中的一个关键概念,它在面向对象编程中扮演着重要的角色。`this` 指针是一个指向当前对象的指针,在成员函数中可以通过 `this` 指针来访问对象的成员变量和成员函数[^1]。 #### 解决名称冲突 当成员变量与函数参数同名时,可以使用 `this` 指针来区分它们。例如: ```cpp class Person { public: int age; Person(int age) { this->age = age; // 使用 this 指针明确指定成员变量 } }; ``` #### 访问对象自身 在成员函数内部,如果需要返回当前对象本身,可以使用 `*this` 来实现。例如: ```cpp class A { private: int x; public: A& setX(int x) { this->x = x; return *this; // 返回当前对象以支持链式调用 } }; ``` ### 使用方法 #### 获取对象地址 在成员函数中,可以通过 `this` 指针获取当前对象的地址。例如: ```cpp class A { public: void display() { std::cout << this << std::endl; // 输出当前对象的地址 } }; ``` #### 成员变量访问 通过 `this` 指针可以直接访问对象的成员变量。例如: ```cpp class A { private: int x; public: void display() { std::cout << this->x << std::endl; // 通过 this 指针访问成员变量 std::cout << (*this).x << std::endl; // 通过解引用 this 指针访问成员变量 } }; ``` #### 级联操作 利用 `*this` 可以实现级联操作,即连续调用成员函数。例如: ```cpp class A { private: int x; public: A& addX(int x) { this->x += x; return *this; // 支持链式调用 } void display() { std::cout << x << std::endl; } }; int main() { A a; a.setX(5).addX(10).display(); // 链式调用 } ``` ### 注意事项 - `this` 指针只有在成员函数中才有定义,创建一个对象后,不能通过对象直接使用 `this` 指针。 - 在类的非静态成员函数中返回类对象本身时,可以使用圆点运算符 `.` 或箭头运算符 `->`。 - `this` 指针的值不能被修改,但可以通过 `this` 指针修改其所指向的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值