this指针

发布时间 2023-11-07 22:46:43作者: zznic

1、this指针指向被调用成员函数所属的对象。

当一个对象被创建后,它的每一个成员函数都含有一个系统自动生成的隐含指针 this,用以保存这个对象的地址,也就是说虽然我们没有写上 this 指针,编译器在编译的时候也是会加上的。This 指针无需定义,直接使用即可。

因此 this 也称为“指向本对象的指针”,this 指针并不是对象的一部分,不会影响 sizeof(对象)的结果。 

this 指针是 C++实现封装的一种机制,它将对象和该对象调用的成员函数连接在一起,在外部看来,每一个对象都拥有自己的函数成员。

注意:

静态成员函数内部没有 this 指针,静态成员函数不能操作非静态成员变量。

this 指针永远指向当前对象。

2、this 指针的使用

    当形参和成员变量同名时,可用 this 指针来区分在类的非静态成员函数中返回对象本身,可使用 return *this.

 

class Person{
public:
    //1. 当形参名和成员变量名一样时,this 指针可用来区分
    Person(string name,int age){
        //name = name;
        //age = age; //输出错误
        this->name = name;
        this->age = age;
    }
    //2. 返回对象本身的引用
    //重载赋值操作符
    //其实也是两个参数,其中隐藏了一个 this 指针
    Person PersonPlusPerson(Person& person){
        string newname = this->name + person.name;
        int newage = this->age + person.age;
        Person newperson(newname, newage);
        return newperson;
    }
void ShowPerson(){
    cout << "Name:" << name << " Age:" << age << endl;
    }
public:
    string name;
    int age;
};
//3. 成员函数和全局函数(Perosn 对象相加)
Person PersonPlusPerson(Person& p1,Person& p2){
    string newname = p1.name + p2.name;
    int newage = p1.age + p2.age;
    Person newperson(newname,newage);
    return newperson;
}
int main(){
    Person person("John",100);
    person.ShowPerson();
    cout << "---------" << endl;
    Person person1("John",20);
    Person person2("001", 10);
    //1.全局函数实现两个对象相加
    Person person3 = PersonPlusPerson(person1, person2);
    person1.ShowPerson();
    person2.ShowPerson();
    person3.ShowPerson();
    //2. 成员函数实现两个对象相加
    Person person4 = person1.PersonPlusPerson(person2);
    person4.ShowPerson();
    system("pause");
    return EXIT_SUCCESS;
}

 

 const 修饰成员函数

用 const 修饰的成员函数时,const 修饰 this 指针指向的内存区域,成员函数体内不可以修改本类中的任何普通成员变量, 当成员变量类型符前用 mutable 修饰时例外。

    //const 修饰成员函数
class Person{
    public:
    Person(){
    this->mAge = 0;
    this->mID = 0;
}
    //在函数括号后面加上 const,修饰成员变量不可修改,除了 mutable 变量
void sonmeOperate() const{
    //this->mAge = 200; //mAge 不可修改
    this->mID = 10;
}
    void ShowPerson(){
        cout << "ID:" << mID << " mAge:" << mAge << endl;
}
    private:
    int mAge;
    mutable int mID;
};
int main(){
    Person person;
    person.sonmeOperate();
    person.ShowPerson();
    system("pause");
    return EXIT_SUCCESS;
}

const 修饰对象(常对象) 常对象只能调用 const 的成员函数 常对象可访问const 或非 const 数据成员,不能修改,除非成员用 mutable 修饰 

class Person{
public:
    Person(){
    this->mAge = 0;
    this->mID = 0;
}
void ChangePerson() const{
    mAge = 100;
    mID = 100;
}
void ShowPerson(){
    this->mAge = 1000;
    cout << "ID:" << this->mID << " Age:" << this->mAge << endl;
}
public:
    int mAge;
    mutable int mID;
};
void test(){
    const Person person;
    //1. 可访问数据成员
    cout << "Age:" << person.mAge << endl;
    //person.mAge = 300; //不可修改
    person.mID = 1001; //但是可以修改 mutable 修饰的成员变量
    //2. 只能访问 const 修饰的函数
    //person.ShowPerson();
    person.ChangePerson();
}