C++this指针

本文深入探讨了C++中类的实现细节,包括成员变量和成员函数的存储方式、解决名称冲突的方法、this指针的使用及不同返回类型的影响等关键技术点。

 

#include <iostream>

using namespace std;

//成员变量和成员函数是分开存储的
class Person {

};


void test() {
    Person p;
    cout << "sizeof p=" << sizeof(p) << endl;
}

int main() {
    test();
    return 0;
}

 输出 sizeof p=1

#include "iostream"

using namespace std;

//成员变量和成员函数是分开存储的
class Person {
public:
    int mb;//非静态成员变量  属于类的对象上
    static int ma;//静态成员变量  不属于类对象上
    void test() {}//非静态成员函数 不属于类对象上
    static void test2() {}//静态成员函数 不属于类对象上
};

int Person::ma = 10;//必须  静态变量需要在类外初始化

void test() {
    Person p;
    cout << "sizeof p=" << sizeof(p) << endl;
}

int main() {
    test();
    return 0;
}

输出

sizeof p=4//其实就是 int mb;的空间

 

#include "iostream"

using namespace std;

//1 解决名称冲突

class Person {
public:
    Person(int age) {
        //age = age;
        //this 指针指向 被调用的成员函数 所属的对象
        this->age = age;
    }

    Person *addAge(int age) {
        this->age += age;
        return this;
    }

    int age;
};

void test() {
    Person p(18);
    cout << "age=" << p.age << endl;
}

int main() {
//    test();
    Person p(10);
    p.addAge(10)->addAge(10);
    cout << p.age << endl;
    return 0;
}

输出

30

重要:

#include "iostream"

using namespace std;

//1 解决名称冲突

class Person {
public:
    Person(int age) {
        //age = age;
        //this 指针指向 被调用的成员函数 所属的对象
        this->age = age;
    }

    Person *addAge(int age) {
        this->age += age;
        return this;
    }

    Person &addAge2(int age) {
        this->age += age;
        return *this;
    }

    Person addAge3(int age) {
        this->age += age;
        return *this;
    }

    int age;
};

void test() {
    Person p(18);
    cout << "age=" << p.age << endl;
}

int main() {
//    test();
    Person p(10);
    p.addAge(10)->addAge(10);
    cout << p.age << endl;

    Person p2(10);
    p2.addAge2(10).addAge2(10);
    cout << p2.age << endl;

    Person p3(10);
    //调用第一次后返回的是个p3的拷贝,所以第一次调用后,再调用都不是本体操作,所以p3.age=20
    p3.addAge3(10).addAge3(10).addAge3(10);
    cout << p3.age << endl;
    return 0;
}
输出
30
30
20

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值