SRo*_*mes 3 c++ constructor scope initialization initializer-list
我知道许多人使用私有成员变量名称的前缀或后缀。对于那些不这样做,但只使用名称的人 - 如果您想要具有相同名称的构造函数参数,如何初始化它们?
通过简单地编写它们。语言规则可以防止出现问题。
struct Foo
{
Foo(int x) : x(x) {};
int x;
};
Run Code Online (Sandbox Code Playgroud)
在 之外()
,只有数据成员在范围内;在内部,函数参数隐藏成员,就像在普通函数体中一样:
int x = 2;
void foo(int x)
{
// any access to `x` means the argument
}
Run Code Online (Sandbox Code Playgroud)
这是我在命名数据成员时不使用m_
前缀样式(或等效样式)的众多原因之一。