关于this指针总结

1. this指针的用处:

  一个对象的this指针并不是对象本身的一部分,不会影响sizeof(对象)的结果。this作用域是在类内部,当在类的非静态成员函数中访问类的非静态成员的时候,编译器会自动将对象本身的地址作为一个隐含参数传递给函数。也就是说,即使你没有写上this指针,编译器在编译的时候也是加上this的,它作为非静态成员函数的隐含形参,对各成员的访问均通过this进行。

2. this指针的使用:

1.在类的非静态成员函数中返回类对象本身的时候,直接使用 return *this;

 

#include <iostream>

using namespace std;

class Test;
void fun(Test *t);

class Test{
    int a;
    int b;
public:
    Test(int x,int y):a(x),b(y){}
    void print(){
        cout<<"a="<<a<<endl;
        cout<<"b="<<b<<endl;
    }
    Test mul()
    {
        this->a=this->a*2;
        this->b=this->b*3;
        return *this;
    }
};

int main(int argc,char**argv)
{
        Test test(1,2);
        Test test1=test.mul();//默认赋值运算符重载
        test1.print();
        return 0;
}

2.当参数与成员变量名相同时,如this->n = n (不能写成n = n);

#include <iostream>
using namespace std;

class Test;
void fun(Test *t);

class Test{
    int a;
    int b;
public:
    Test(int x,int y):a(x),b(y){}
    void print(){
        cout<<"a="<<a<<endl;
        cout<<"b="<<b<<endl;
    }
    int mul(int a=1)
    {
        this->a=this->a*a;
        return this->a;//20
     //return a;//10
    }
};

int main(int argc,char**argv)
{        
  Test test(2,3);
    int result=test.mul(10);
    cout<<"result="<<result<<endl;
    return 0;
}

ps:尽量保证形参名不同于成员变量名

3.成员函数可以传递this指针给非成员函数;

#include <iostream>
using namespace std;

class Test;
void fun(Test *t);

class Test{
    int a;
    int b;
public:
    Test(int x,int y):a(x),b(y){}
    void print(){
        cout<<"a="<<a<<endl;
        cout<<"b="<<b<<endl;
    }

    void call()
    {
        fun(this);
    }
};

void fun(Test *t)
{
    t->print();
}
int main(int argc,char**argv)
{
        Test test(1,2);
        test.call();
        return 0;
}

 成员函数call传递实例化对象test所在地址给fun函数,fun函数使用this指针调用该对象的成员函数

3. this指针程序示例:

this指针存在于类的成员函数中,指向被调用函数所在的类实例的地址。

#include<iostream>
using namespace std;  
class Point
{   
  int x, y; 
public:   
  Point(int a, int b) { x=a; y=b;}   
  void MovePoint( int a, int b){ x+=a; y+=b;}   
  void print(){ cout<<"x="<<x<<"y="<<y<<endl;} <="" font="">  
};   
void main()   
{   
  Point point1( 10,10);   
  point1.MovePoint(2,2);   
  point1.print();   
}   

当对象point1调用MovePoint(2,2)函数时,即将point1对象的地址传递给了this指针。   

MovePoint函数的原型应该是 

void MovePoint( Point *this, int a, int b);

第一个参数是指向该类对象的一个指针,它是隐含的

void MovePoint(int a, int b) { this->x +=a; this-> y+= b;} 

即可以知道,point1调用该函数后,也就是point1的数据成员被调用并更新了值。即该函数过程可写成

point1.x+= a; point1. y + = b;

调用过程应为:

  point1.MovePoint(&point1,2,2); 

5. 类的this指针有以下特点:

1this只能在成员函数中使用。

全局函数、静态函数都不能使用this.

实际上,成员函数默认第一个参数为T * const this。

如: 

class A
 {
  public:
     int func(int p){}
 };

其中,func的原型在编译器看来应该是:

  int func(A * const this,int p);

2)由此可见,this在成员函数的开始前构造,在成员函数的结束后清除。

这个生命周期同任何一个函数的参数是一样的,没有任何区别。

当调用一个类的成员函数时,编译器将类的指针作为函数的this参数传递进去。如:

A a;
a.func(10);

此处,编译器将会编译成:

A::func(&a,10);

转载:https://www.cnblogs.com/Star-Lit/p/8623050.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
运算符重载是指在类中对C++内置运算符进行重新定义的过程,使得运算符能够适用于类对象。C++中的运算符重载可以通过成员函数和非成员函数两种方式实现。 this指针是一个指向当前对象的指针,它在成员函数中使用。在运算符重载中,this指针可以用来引用当前对象的成员变量和成员函数。 例如,在一个矩形类中重载“+”运算符,可以通过以下两种方式实现: 1. 成员函数方式: ```c++ class Rectangle { public: int width, height; Rectangle operator+(const Rectangle& other) { Rectangle result; result.width = this->width + other.width; result.height = this->height + other.height; return result; } }; ``` 在上述代码中,重载了“+”运算符,并使用了成员函数的方式实现。在函数中使用了this指针来引用当前对象的成员变量。 2. 非成员函数方式: ```c++ class Rectangle { public: int width, height; }; Rectangle operator+(const Rectangle& r1, const Rectangle& r2) { Rectangle result; result.width = r1.width + r2.width; result.height = r1.height + r2.height; return result; } ``` 在上述代码中,重载了“+”运算符,并使用了非成员函数的方式实现。在函数中没有使用this指针,而是直接引用了两个参数对象的成员变量。 总结:运算符重载中可以使用this指针来引用当前对象的成员变量和成员函数。但是,在非成员函数的方式中无法使用this指针,需要通过参数来引用对象的成员变量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值