c++day7 this指针

本文详细介绍了C++中的`this`指针,包括其作用、如何在成员函数中使用,以及`const`关键字与`this`指针的结合。通过示例展示了`this`在成员函数和友元函数中的应用,以及`this`在运算符重载中的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++:this
void setXY(Simple * const this,int a,int b){
    this->x = a;this->y = b;
}
其实也就是obj1.setXY(10,15);
obj1.setXY(10,15,&obj1);//有一个默认的指针(引用)指向当前的对象,用this也可以区分实名的形参和实参

#include <iostream>
using namespace std;
//请问这里三个不同位置的const分别修饰的是什么?
//1.const修饰的其实是this指针,只不过c++编译器隐藏起来了。
//const修饰的不是指针, 是指针所指向的内存空间,可以改变内存空间的值
//但是不可以直接修改this指向的内存空间
//2.例如this=0x11;是不允许这样修改的。
class Test{
public :
    Test(int a=0 ,int b=4 ){
        this->a = a;
        this->b = b;
    }
    int getA(){
        return a ;
    }
    int getB(){
        return b;
    }
public:
    //t3 = t1.TestAdd(t2);
    Test TestAdd(Test &t2){
        Test tmp(this->a+t2.a,this->b+t2.b);
        return tmp;
    }
    //t1 = t2 + t1;函数的运算结果累加到自身上
    //返回一个引用相当于返回变量的自身,就是返回的内存的首地址,返回t1这个元素
    Test &TestAdd2(Test &t2){
        this->a = this->a + t2.a;
        this->b = this->b + t2.b;
        cout<<"调用返回自身的累加函数"<<endl;
        return *this;//把*(&t1)又回到了t1元素
    }

    void printT(){
        cout<<"a"<<a<<"b"<<b<<"这是调用的成员函数"<<endl;
    }
    ~Test(){
        cout<<"a"<<a<<"b"<<b<<endl;
        cout<<"析构函数被调用"<<endl;
    }
protected:
private:
    int a;
    int b;
};

const void Opvar(int a,int b){

}
 void const Opvar1(int a,int b){

}
 /*
 void Opvar2(int a,int b)const{ ==>void Opvar2(const int *this,int a,int b)
 //const修饰的不是指针, 是指针所指向的内存空间
}*/

 //全局函数的方法
 Test TestAdd(Test &t1,Test &t2){
     Test tmp;
     return tmp;
 }
     void printT(Test *pt){
        cout<<"a"<<pt->getA()<<"b"<<pt->getB()<<"这是调用的全局函数"<<endl;
    }

void main(){
    Test t1(2,4);
    Test t2(3,4);
    Test t3;
    //全局函数方法
    t3 = TestAdd(t1,t2);

    //先写测试案例
    {
        //成员函数方法
        Test t4 = t1.TestAdd(t2);
        Test t5;
        t5 = t1.TestAdd(t2);//匿名对象赋值给t5
        t5.printT();
        printT(&t5);
    }
    t1.TestAdd2(t2);
    t1.printT();
    cout<<"hello"<<endl;
    system("pause");
    return ;
}

//数组类的封装
#include <iostream>
using namespace std;
class Array{
public:
    Array(int len){
        if(len < 0 ){
            mlen =  0;
        
        }
            mlen = len;
            mspace = new int[mlen];
    }
    Array(const Array &obj){
        this->mlen = obj.mlen;
        this->mspace =  new int [mlen];//分配内存空间
        for(int i=0;i<obj.mlen;i++){ //数据元素赋值
            this->mspace[i]= obj.mspace[i];
        }

    }
    ~Array(){
        if(mspace != NULL){
            delete[] mspace;
            mlen = 0;
        }
    }
public:
    void setData(int index,int value){
        mspace[index] = value;
    }
    int getData(int index){
        return mspace[index];
    }
    int length(){
        return mlen;
    }
protected:
private:
    int mlen;
    int *mspace;
};


void main(){

    Array a1(10);
    for(int i=0;i<a1.length();i++){
        a1.setData(i,i);
    }
    cout<<"输出a1"<<endl;
    for(int i=0;i<a1.length();i++){
        cout<<a1.getData(i)<<endl;;
    }
    Array a2 = a1;
    cout<<"输出a2"<<endl;
    for(int i=0;i<a2.length();i++){
        cout<<a2.getData(i)<<endl;;
    }

 

    cout<<"hello"<<endl;
    system("pause");
    return ;
}

//友元函数
#include <iostream>
using namespace std;


class A{
public:
    A(int a,int b){
        this->a = a;
        this->b = b;
    }
    //友元函数可以访问私有属性
    friend void modifyA(A *pa,int m);//声明的位置和private没关系
    int getA(){
        return a ;
    }
protected:
private:
    int a;
    int b ;
};
void modifyA(A *pa,int m){
    pa->a = m;
}

void main(){
    A a1(1,2);
    cout<<a1.getA()<<endl;
    modifyA(&a1,200);
    cout<<a1.getA()<<endl;
    cout<<"hello"<<endl;
    system("pause");
    return ;
}


//友元类
//一般有特定的应用场景,没有友元函数用的多,过于复杂的很少见

#include <iostream>
using namespace std;


class A{
public:
    A(int a=0,int b=0){
        this->a = a;
        this->b = b;
    }
    //友元函数可以访问私有属性
    friend void modifyA(A *pa,int m);//声明的位置和private没关系
    int getA(){
        return a ;
    }
public:
    friend class B;//B类是A类的好友,在B类中可访问A类私有成员
    //声明的位置和public private没关系
protected:
private:
    int a;
    int b ;
};

class B{
public:
    void set(int  a){
        Aobj.a = a;
    }
    void printB(){
        cout<<Aobj.a<<endl;
    }
protected:
private:
    A  Aobj;
};

void modifyA(A *pa,int m){
    pa->a = m;
}

void main(){
//    A a1(1,2);
//    cout<<a1.getA()<<endl;
//    modifyA(&a1,200);
//    cout<<a1.getA()<<endl;
    B b1;
    b1.set(300);
    b1.printB();

    cout<<"hello"<<endl;
    system("pause");
    return ;
}


//为什么要设计友元类呢?
//1.java - 2.class字节码 ==》反射机制通过class找到类对象,直接改私有属性
//反射机制已经成为了一种标准,jdk,api,gcc

 

//运算符重载,语法较难
#include <iostream>
using namespace std;

class Complex
{
public:
    int a;
    int b;
public:
    Complex(int a=0,int b=0){
        this->a = a;
        this->b = b;
    }
    void printCom(){
        cout<<a<<"  +"<<b<<"i"<<endl;
    }
protected:
private:

};
//定义全局函数
Complex myAdd(Complex &c1,Complex &c2){
        Complex tmp(c1.a+c2.a,c1.b+c2.b);
        return tmp;
    }

//函数名升级
Complex operator+(Complex &c1,Complex &c2){
        Complex tmp(c1.a+c2.a,c1.b+c2.b);
        return tmp;
    }
void main(){

    int a =0,b = 0;
    int c;
    c = a+ b;//基础数据类型 编译器已经知道怎么运算了

    Complex c1(1,2),c2(3,4);
    //a+bi 复数运算规则
    Complex c3;
    c3 = c1+ c2;//类 也是一种数据类型  用户自定义数据类型 C++编译器 是不知道如何进行运算

    //c++编译器器给程序员提供一种机制
    //运算符重载机制
    
    //步骤1
    //Complex c4 = myAdd(c1,c2);
    //c4.printCom();

    //步骤2  Complex c4 = c1+c2;
    //Complex c4 = operator+(c1,c2);
    //c4.printCom();

    //步骤3
    Complex c4 = c1+c2;
    c4.printCom();

    //总结:运算符重载的本质是函数调用
    //不能重载的运算符: . ::  .* ?: sizeof


    cout<<"hello"<<endl;
    system("pause");
    return ;
}

//运算符函数可以重载为成员函数
//一元运算符和二元运算符,一元比二元要难些。
//类成员函数和全局函数方法都可以实现操作符重载
//1.确认函数逻辑
//2.根据操作数,写出函数参数
//3.根据业务,完善函数返回值,看返回引用还是返回值,实现函数业务

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值