C++ 中使用下划线的约定类成员名称
我的大部分编程知识都是自学的,所以我从未被教导过正确的设计模式、约定等等。
我最近一直在挖掘公司的很多软件库,我注意到很多类数据成员的名称中都有下划线。
例如:
class Image
{
// various things
// data members
char* _data;
ImageSettings* _imageSettings;
// (and so on...)
};
我也在很多在线示例代码中看到了这一点。这个约定背后有什么原因吗?抱歉,我无法提供更好的例子,我真的很努力地记住,但我看到了很多。
我知道匈牙利表示法,但我试图掌握用于 C++ OOP 编程的所有其他约定。
The large majority of my programming knowledge is self-taught, so I was never taught proper design patterns, conventions, and so on and so forth.
I've been digging through a lot of my company's software libraries lately, and I notice that a lot of class data members have underscores in their names.
For example:
class Image
{
// various things
// data members
char* _data;
ImageSettings* _imageSettings;
// (and so on...)
};
I see this in a lot of online example code as well. Is there a reason behind this convention? Sorry I couldn't provide better examples, I'm really trying to remember off the top of my head, but I see it a lot.
I am aware of Hungarian notation, but I am trying to get a handle on all of the other conventions used for C++ OOP programming.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它只是为了明确哪些变量是成员,哪些不是。没有什么更深层次的意义。实现成员函数时混淆的可能性较小,并且您可以选择更简洁的名称。
就我个人而言,我将下划线放在末尾而不是开头[如果您不小心在前导下划线后面加上大写字母,则您的代码格式不正确]。有些人输入
mBlah
或m_Blah
。其他人则根本不执行任何操作,并在可能发生混淆时显式使用this->blah
来指示其成员变量。没有全球标准;只为私人项目做你想做的事,并遵守其他地方的现有做法。It is simply intended to make it clear what variables are members, and which are not. There is no deeper meaning. There is less possibility for confusion when implementing member functions, and you are able to chose more succinct names.
Personally, I put the underscore at the end instead of the begining [if you accidently follow a leading underscore with a capital letter, your code becomes ill formed]. Some people put
mBlah
orm_Blah
. Others do nothing at all, and explicitly usethis->blah
to indicate their member variables when confusion is possible. There is no global standard; just do what you want for private projects, and adhere to the existing practices elsewhere.我见过在成员前面输入
_
,只是为了通知读者它是一个类成员变量。我见过的更传统的方法是放置
m_
;即m_data
;I have seen
_
typing in front of the member, just to notify the reader that it's a class member variable.More conventional way I have seen is putting
m_
; i.e.m_data
;当成员是私有的时,通常在成员之前使用下划线。
它在没有内置方法来声明成员私有的语言中很有用,比如 python。
Usually underscore before a member is used when the member is private.
It is usefull in language that does not have a builtin way to declare members private, like python.
成员变量中通常使用下划线来区分成员变量、静态成员变量和成员变量。局部变量。
m_
用于普通成员变量 &s_
用于静态成员变量这样变量的范围在变量名称本身中可见。
另外,有时成员名称中会使用下划线,以便您可以使用成员名称本身来命名
get
和set
方法。例如:
然而,不同的组织采用不同的约定和规则。编码风格,并且应该坚持它们。遵循原则,
在罗马时思考&像罗马人一样行事:)
Usually an underscore is used in member variables so as to distinguish between member variables, static member variables & local variables.
m_
is used for normal member variables &s_
for static member variablesThis way the scope of the variable is visible in the variable name itself.
Also, sometimes underscores are used in member name so that you can name your
get
andset
methods with the member name itself.For example:
However, different organizations adopt different conventions & coding styles and one should stick to them. Follow the principle,
When in Rome think & act like the Romans :)
我认为命名没有通用的规则。然而,最重要的之一是坚持公司/项目中已经使用的内容。别弄坏它。否则,您的技术主管或导师很可能会就此向您提出质疑。
Google C++ 命名风格供您参考
I don't think there is a universal rule for naming. However, one of the most important one is to stick to what's already used in your company/project. Don't break it. Otherwise, it's most likely that your tech lead or mentor will challenge you about it.
For your reference, Google C++ style for naming
我了解到的是,变量之前或之后有下划线意味着它是该类的成员 - 私有、公共或受保护的。
What I've learned is that having an underscore before or after a variable means that it's a member of that class - private, public, or protected.
这样做的原因是以下划线开头的成员名称首先出现在 Intellisense 中。一般来说,您必须遵循您所贡献的项目的约定。如果您要开始一个新项目,最好遵循普遍接受的约定,例如 Sutter 和 Alexandrescu 的 C++ 编码标准。
The reason for this convention is that member names beginning with underscores show up first in Intellisense. Generally, you have to follow the convention of the project you are contributting to. If you are starting a new project, it is a good idea to follow a commonly accepted convention, such as Sutter's and Alexandrescu's C++ Coding Standards.