您当前的位置: 首页 > 

钟钟终

暂无认证

  • 0浏览

    0关注

    233博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

补知识点(this指针,友元,运算符重载)

钟钟终 发布时间:2021-06-02 22:36:38 ,浏览量:0

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

2.解决名称冲突 (形参与命名冲突)

this->age=age   //加以区分,this指向被调用的成员函数所属的对象

3.返回对象本身用 *this

#include 

using namespace std;
class person
{
public:
    int age;
    person(int age)
    {
        this->age=age;
    }
    /*person addperson(person &p) //以值的方式返回
    {
        this->age+=p.age;       //每次调用会返回新的对象(拷贝构造函数)
        return *this;
    }*/
    person& addperson(person &p) //以引用的方式返回,返回本体
    {
        this->age+=p.age;
        return *this;
    }
};
int main()
{
    person p1(20);
    cout            
关注
打赏
1664378814
查看更多评论
0.0412s