C++ 成员变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10198046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Member Variables
提问by Julian
Consider the following class:
考虑以下类:
class A
{
A();
int number;
void setNumber(int number);
};
You could implement 'setNumber' in 3 ways:
您可以通过 3 种方式实现“setNumber”:
Method 1: Use the 'this' pointer.
方法 1:使用“this”指针。
void A::setNumber(int number)
{
this->number = number;
}
Method 2: Use the scope resolution operator.
方法 2:使用范围解析运算符。
void A::setNumber(int number)
{
A::number = number;
}
Method 3: Instead, denote all member variables with 'm' or '_' (this is my preferred method).
方法 3:相反,用 'm' 或 '_' 表示所有成员变量(这是我的首选方法)。
void A::setNumber(int number)
{
mNumber = number;
}
Is this just personal preference, or is there a benefit to choosing a particular method?
这只是个人喜好,还是选择特定方法有好处?
采纳答案by Kerr
This is mostly a personal preference, but let me share my perspective on the issue from inside a company where many small games are being made simultaneously (and so there are many coding styles being used around me).
这主要是个人偏好,但让我从一家同时制作许多小游戏的公司内部分享我对这个问题的看法(因此我周围使用了许多编码风格)。
This link has several good, related, answers: Why use prefixes on member variables in C++ classes
此链接有几个很好的相关答案:Why use prefixes on member variables in C++ classes
Your option 1:
您的选择 1:
void A::setNumber(int number)
{
this->number = number;
}
First off, many programmers tend to find this cumbersome, to continually type out the ' this-> '. Second, and more importantly, if any of your variables shares a name with a parameter or a local variable, a find-replace designed to say, change the name of 'number' might affect the member variables located in the find-replace scope as well.
首先,许多程序员倾向于发现这很麻烦,不断地输入'this->'。其次,更重要的是,如果您的任何变量与参数或局部变量共享一个名称,则设计替换的查找替换可能会影响位于查找替换范围内的成员变量,如好。
Your option 2:
您的选择 2:
void A::setNumber(int number)
{
A::number = number;
}
The problem I've run into with this, is that in large classes, or classes with large functions (where you cannot see the function or the class is named unexpectedly), the formatting of A::(thing) looks very much like accessing a part of a namespace, and so can be misleading. The other issue is the same as #2 from the previous option, if your names are similar to any variables you're using there can be unexpected confusion sometimes.
我遇到的问题是,在大型类或具有大型函数的类中(您无法看到函数或类被意外命名), A::(thing) 的格式看起来非常像访问命名空间的一部分,因此可能会产生误导。另一个问题与上一个选项中的 #2 相同,如果您的名称与您使用的任何变量相似,有时可能会出现意外混淆。
Your option 3:
您的选择 3:
void A::setNumber(int number)
{
mNumber = number;
}
This is the best of those three presented options. By creating (and holding to!) a syntax that involves a clear and meaningful prefix, you not only create a unique name that a local (or global) variable won't share, but you make it immediately clear where that variable is declared, regardless of the context you find it in. I've seen it done both like this ' mVariable ' and like this 'm_variable' and it mostly depends upon if you prefer underscores to uppercase concatenation. In addition, if your style tends to add things like ' p 's on for pointers, or ' g 's on for globals, this style will mesh well and be expected by readers.
这是这三个选项中最好的。通过创建(并坚持!)一个包含清晰且有意义的前缀的语法,您不仅创建了一个本地(或全局)变量不会共享的唯一名称,而且您可以立即明确声明该变量的位置,不管你在什么上下文中找到它。我已经看到它像这样的“mVariable”和像这样的“m_variable”,这主要取决于你是否更喜欢下划线而不是大写连接。此外,如果您的风格倾向于添加诸如 ' p ' 表示指针,或 ' g ' 表示全局变量之类的东西,这种风格会很好地融合并受到读者的期待。
回答by Kerr
It is a matter of style, thus personal preference, or a preference of the team you are working with or the boss of the team you are working with.
这是风格问题,因此是个人偏好,或者是与您合作的团队或与您合作的团队的老板的偏好。
回答by Luchian Grigore
Option 4:
选项 4:
void A::setNumber(int n)
{
number = n;
}
Why is the benefit of using the same name for a member anda parameter. No good can come of this. Sure, it's clear now, but when your methods get large, and the prototype doesn't fit in the screen anymore, and there's some other developer writing code, he may forget to qualify the member.
为什么对成员和参数使用相同名称的好处。这不会有什么好处。当然,现在很清楚,但是当您的方法变大,原型不再适合屏幕时,并且有其他一些开发人员编写代码时,他可能会忘记对成员进行资格认证。
回答by nate_weldon
its all personal preference
这完全是个人喜好
but here is a good discussion on it at a high non language level
但这是一个很好的非语言级别的讨论
https://stackoverflow.com/questions/381098/what-naming-convention-do-you-use-for-member-variables
https://stackoverflow.com/questions/381098/what-naming-convention-do-you-use-for-member-variables
回答by JohnMcG
I would say the choice between Method 1 and Method 3 is a matter of personal or organizational style.
我会说方法 1 和方法 3 之间的选择是个人或组织风格的问题。
Method 2 is an inferior because Class::member typically denotes a static member variable, and thus would cause confusion if used to disambiguate between a parameter and member variable.
方法 2 是次等的,因为 Class::member 通常表示静态成员变量,因此如果用于消除参数和成员变量之间的歧义,则会导致混淆。
回答by JohnMcG
I am going to agree with Luchian by providing a better variant. The other examples you provide are too cumbersome or confusing.
我将通过提供更好的变体来同意 Luchian。您提供的其他示例过于繁琐或令人困惑。
Option 4:
选项 4:
void A::setNumber(int aNumber)
{
theNumber = aNumber;
}
This is part of the coding standard we use at my employer and it is very clear what you are describing. It is not Hungarian notation.
这是我们在我的雇主使用的编码标准的一部分,您所描述的内容非常清楚。它不是匈牙利符号。