C++ 中成员变量的尾随下划线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3650623/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:23:23  来源:igfitidea点击:

Trailing underscores for member variables in C++

c++naming-conventionsmember

提问by eomer

I've seen people use a trailing underscore for member variables in classes, for instance in the renowned C++ FAQ Lite.

我见过人们在类中对成员变量使用尾随下划线,例如在著名的C++ FAQ Lite 中

I think that it's purpose is not to mark variables as members, that's what "m_" is for. It's actual purpose is to make it possible to have an accessor method named like the field, like this:

我认为它的目的不是将变量标记为成员,这就是“m_”的用途。它的实际目的是使具有一个像字段一样命名的访问器方法成为可能,如下所示:

class Foo {
public:
    bar the_bar() { return the_bar_; }
private:
    bar the_bar_;
}

Having accessors omit the "get_" part is common in the STL and boost, and I'm trying to develop a coding style as close to these as possible, but I can't really see them using the underscore trick. I wasn't able to find an accessor in STL or boost that would just return a private variable.

让访问器省略“get_”部分在 STL 和 boost 中很常见,我正在尝试开发一种尽可能接近这些的编码风格,但我无法真正看到它们使用下划线技巧。我无法在 STL 或 boost 中找到只会返回私有变量的访问器。

I have a few questions I'm hoping you will be able to answer:

我有几个问题希望你能回答:

  1. Where does this convention come from? Smalltalk? Objective-C? Microsoft? I'm wondering.
  2. Would I use the trailing underscore for all private members or just as a workaround in case I want to name a function like a variable?
  3. Can you point me to STL or boost code that demonstrates trailing underscores for member variables?
  4. Does anybody know what Stroustrup's views on the issue are?
  5. Can you point me to further discussion of the issue?
  1. 这个约定从何而来?短暂聊天?目标-C?微软?我很好奇。
  2. 我会为所有私有成员使用尾随下划线,还是作为一种解决方法,以防我想将函数命名为变量?
  3. 你能指出我展示成员变量尾随下划线的 STL 或 boost 代码吗?
  4. 有人知道 Stroustrup 对这个问题的看法是什么吗?
  5. 你能指出我对这个问题的进一步讨论吗?

回答by sbi

In C++,

在 C++ 中,

  1. identifiers starting with an underscore, followed by a capital character
  2. identifiers having two consecutive underscores anywhere
  3. identifiers in the global namespace starting with an underscore
  1. 标识符以下划线开头,后跟大写字符
  2. 任何地方都有两个连续下划线的标识符
  3. 全局命名空间中以下划线开头的标识符

are reserved to the implementation. (More about this can be found here.) Rather than trying to remember these rules, many simply do not use identifiers starting with an underscore. That's why the trailing underscore was invented.

保留给执行。(可以在此处找到更多相关信息。)与其试图记住这些规则,不如说许多规则根本不使用以下划线开头的标识符。这就是发明尾随下划线的原因。

However, C++ itself is old, and builds on 40 years of C (both of which never had a single company behind them), and has a standard library that has "grown" over several decades, rather than brought into being in a single act of creation. This makes for the existence of a lot of differing naming conventions. Trailing underscore for privates (or only for private data) is but one, many use other ones (not few among them arguing that, if you need underscores to tell private members from local variables, your code isn't clear enough).

然而,C++ 本身已经很老了,并且建立在 40 年的 C 基础上(两者都没有一个公司支持它们),并且有一个标准库,它已经“成长”了几十年,而不是一蹴而就的创作。这导致存在许多不同的命名约定。私有(或仅用于私有数据)的尾随下划线只是一种,许多使用其他下划线(其中不少人认为,如果您需要下划线来告诉私有成员和局部变量,那么您的代码不够清楚)。

As for getters/setters - they are an abomination, and a sure sign of "quasi classes", which I hate.

至于吸气剂/二传手——它们是可憎的,是我讨厌的准类的明确标志。

回答by forceal

I've read The C++ Programming Languageand Stroustrup doesn't use any kind of convention for naming members. He never needs to; there is not a single simple accessor/mutator, he has a way of creating very fine object-oriented designs so there's no need to have a method of the same name. He uses structs with public members whenever he needs simple data structures. His methods always seem to be operations. I've also read somewhere that he disencourages the use of names that differ only by one character.

我读过The C++ Programming Language并且 Stroustrup 不使用任何命名成员的约定。他从来不需要;没有一个简单的访问器/修改器,他有一种创建非常好的面向对象设计的方法,因此不需要有一个同名的方法。每当他需要简单的数据结构时,他都会使用带有公共成员的结构。他的方法似乎总是操作。我还在某处读到过,他不鼓励使用仅相差一个字符的名称。

回答by identity

I am personally a big fan of this guideline: http://geosoft.no/development/cppstyle.html

我个人是这个指南的忠实粉丝:http: //geosoft.no/development/cppstyle.html

It includes omitting the m_ prefix, using an underscore suffix to indicate private member variables and dropping the horrid, annoying-to-type habit of using underscores instead of space, and other, more detailed and specific suggestions, such as naming bools appropriately(isDoneinstead of just done) and using getVariable()instead of just variable()to name a few.

它包括省略 m_ 前缀,使用下划线后缀来指示私有成员变量并删除使用下划线而不是空格的可怕的、令人讨厌的打字习惯,以及其他更详细和具体的建议,例如适当地命名 bools(isDone而不是只是done) 并使用getVariable()而不是variable()仅举几例。

回答by usta

Only speaking for myself... I always use trailing underscore for private data members, regardless if they have accessor functions or not. I don't use m_ mainly because it gets in the way when I mentally spell the variable's name.

仅代表我自己...我总是对私有数据成员使用尾随下划线,无论它们是否具有访问器功能。我不使用 m_ 主要是因为它会妨碍我拼写变量的名称。

回答by Just another metaprogrammer

As a maintenance developer that likes searchability I'm leaning towards m_as its more searchable. When you, as me, are maintaining big projects with large classes (don't ask) you sometimes wonder: "Hmmm, who mutates state?". A quick search for m_can give a hint.

作为喜欢可搜索性的维护开发人员,我倾向于m_它更易于搜索。当你和我一样,维护大类的大项目时(不要问),你有时会想:“嗯,谁改变了状态?”。快速搜索m_可以给出提示。

I've also been known to use l_to indicate local variables but the current project doesn't use that so I'm "clean" these days.

我也被l_用来表示局部变量,但当前项目不使用它,所以我这些天很“干净”。

I'm no fan of hungarian notation. C++ has a strong type system, I use that instead.

我不喜欢匈牙利符号。C++ 有一个强类型系统,我用它来代替。

回答by StuartLC

I'm guessing that utopia would have been to use a leading underscore - this is quite common in Java and C# for members.

我猜乌托邦应该使用前导下划线 - 这在 Java 和 C# 中对于成员来说很常见。

However, for C, leading underscores aren't a good idea, so hence I guess the recommendation by the C++ FAQ Lite to go trailing underscore:

但是,对于 C,前导下划线不是一个好主意,因此我猜 C++ FAQ Lite 建议使用尾随下划线:

All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.

All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.


(ISO C99 specification, section 7.1.3)

所有以下划线和大写字母或另一个下划线开头的标识符始终保留供任何使用。

所有以下划线开头的标识符始终保留用作普通名称空间和标记名称空间中具有文件范围的标识符。


(ISO C99 规范,第 7.1.3 节)

回答by Stephane Rolland

As far as I remember, it's not Microsoft that pushed the trailing underscore code style for members.

据我所知,推动成员使用尾随下划线代码风格的并不是微软。

I have read that Stroustrup is pro the trailing underscore.

我读过 Stroustrup 支持尾随下划线。