如果参数与数据成员同名怎么办?

What if an argument has the same name as that of a data member?

#include <iostream>

struct A
{
    A(int n) { std::cout << n; }
    int n{2};
};

int main()
{
    A a{1};
}

输出是 1 而不是 2

C++ 标准是否定义参数名称与数据成员的名称相同时优先?

参数比成员变量在"closer"范围内,所以参数shadows成员变量。

显而易见的解决方案是重命名参数(或成员变量),因此它们不再相同。

也可以使用this->n显式使用成员变量