在c++中,成员变量的尾部下划线

时间:2022-03-27 13:18:08

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. 这个公约从何而来?Smalltalk吗?objective - c ?微软?我想知道。
  3. 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?
  4. 我是否会为所有的私有成员使用后面的下划线,或者只是作为一个工作区,以备我想要将一个函数命名为变量?
  5. Can you point me to STL or boost code that demonstrates trailing underscores for member variables?
  6. 您能给我指出STL或boost代码来演示成员变量的下划线吗?
  7. Does anybody know what Stroustrup's views on the issue are?
  8. 有人知道Stroustrup对这个问题的看法吗?
  9. Can you point me to further discussion of the issue?
  10. 你能让我进一步讨论这个问题吗?

7 个解决方案

#1


26  

In C++,

在c++中,

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

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 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.

至于getter /setter,它们是令人憎恶的,是“准类”的确切标志,我讨厌它。

#2


15  

I've read The C++ Programming Language and 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.

我读过c++编程语言,Stroustrup不使用任何类型的约定来命名成员。他从不需要;没有一个简单的访问器/mutator,他有一种创建非常好的面向对象设计的方法,所以不需要有同名的方法。每当他需要简单的数据结构时,他都会使用结构体和公共成员。他的方法似乎总是操作。我也在某些地方读到过,他不鼓励只使用一个字符的名字。

#3


12  

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(isDone instead of just done) and using getVariable() instead of just variable() to name a few.

使用下划线包括省略m_前缀,后缀表明私有成员变量和下降的可怕,annoying-to-type用下划线代替空间的习惯,和其他,更详细和具体的建议,例如命名bool适当(结束而不是做)和使用getVariable()()而不是变量等等。

#4


8  

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_,主要是因为它妨碍了我在脑海中拼写变量名。

#5


4  

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++有一个强大的类型系统,我用它来代替。

#6


3  

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节)

#7


1  

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是支持结尾下划线的。

#1


26  

In C++,

在c++中,

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

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 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.

至于getter /setter,它们是令人憎恶的,是“准类”的确切标志,我讨厌它。

#2


15  

I've read The C++ Programming Language and 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.

我读过c++编程语言,Stroustrup不使用任何类型的约定来命名成员。他从不需要;没有一个简单的访问器/mutator,他有一种创建非常好的面向对象设计的方法,所以不需要有同名的方法。每当他需要简单的数据结构时,他都会使用结构体和公共成员。他的方法似乎总是操作。我也在某些地方读到过,他不鼓励只使用一个字符的名字。

#3


12  

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(isDone instead of just done) and using getVariable() instead of just variable() to name a few.

使用下划线包括省略m_前缀,后缀表明私有成员变量和下降的可怕,annoying-to-type用下划线代替空间的习惯,和其他,更详细和具体的建议,例如命名bool适当(结束而不是做)和使用getVariable()()而不是变量等等。

#4


8  

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_,主要是因为它妨碍了我在脑海中拼写变量名。

#5


4  

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++有一个强大的类型系统,我用它来代替。

#6


3  

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节)

#7


1  

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是支持结尾下划线的。