函数参数和数据成员同名

#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
class boy
{
    string name;
    int  age;
   public:
    boy(string name,int age)
    {
        name=name;
        age=age;
        this->name=name;//当函数参数和数据成员同名时,函数参数的优先级高,所以优先
        boy::age=age;//访问的是函数参数,利用this 或者作用域来修改

    }
    void get()
    {
        cout<<this->name<<" "<<this->age<<endl;
    }
};
int main()
{
    boy a("guo",22);
    a.get();
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/cs1003/p/2814443.html