构造函数参数和类的数据成员同名时的两种写法

#include <iostream>

using namespace std;

class Base
{
public:
    Base(int x) : x(x)//成员初始化列表中不需要也不能够用this指针指出数据成员
    {

    }

    int x;
};

class Base1
{
public:
    Base1(int x)
    {
        this->x = x;//函数体里赋值时必须用this指明那个是类的数据成员
    }

    int x;
};


int main()
{
    Base b(4);
    cout << b.x << endl;

    return 0;
}

  

posted on 2016-10-08 10:53  lakeone  阅读(1879)  评论(0编辑  收藏  举报

导航