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

发布于 2024-09-18 00:51:28 字数 677 浏览 11 评论 0原文

我见过人们在类中使用尾随下划线作为成员变量,例如在著名的 C++ 常见问题解答精简版

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

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

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

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

  1. 这个约定从何而来?闲聊?目标-C?微软?我很好奇。
  2. 我是否会对所有私有成员使用尾随下划线,或者只是作为一种解决方法,以防我想将函数命名为变量?
  3. 你能给我指出演示成员变量尾部下划线的 STL 或 boost 代码吗?
  4. 有人知道 Stroustrup 对这个问题的看法吗?
  5. 你能指出我对这个问题的进一步讨论吗?

I've seen people use a trailing underscore for member variables in classes, for instance in the renowned 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:

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

迷乱花海 2024-09-25 00:51:28

在 C++ 中,

  1. 以下划线开头的标识符,后跟具有
  2. 两个连续下划线的大写字符标识符,
  3. 全局命名空间中以下划线开头的任何标识符

都保留给实现。 (有关此内容的更多信息,请参见此处。)许多人并没有尝试记住这些规则,而是根本不使用以一个下划线。这就是发明尾随下划线的原因。

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

至于 getters/setters - 它们是令人厌恶的,并且是 "准类",我讨厌它。

In 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

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

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

没︽人懂的悲伤 2024-09-25 00:51:28

我读过《C++ 编程语言》,Stroustrup 没有使用任何类型的命名成员的约定。他从来不需要这样做;没有一个简单的访问器/修改器,他有一种创建非常精细的面向对象设计的方法,因此不需要有同名的方法。每当需要简单的数据结构时,他都会使用带有公共成员的结构。他的方法似乎总是操作。我还在某处读到,他不鼓励使用仅一个字符不同的名称。

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.

等待我真够勒 2024-09-25 00:51:28

我个人非常喜欢这个指南:http://geosoft.no/development/cppstyle.html

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

I am personally a big fan of this guideline: 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.

悍妇囚夫 2024-09-25 00:51:28

只为我自己说话...
我总是对私有数据成员使用尾随下划线,无论它们是否具有访问器函数。我不使用 m_ 主要是因为当我在心里拼写变量名称时它会妨碍我。

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.

赤濁 2024-09-25 00:51:28

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

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

我不喜欢匈牙利表示法。 C++ 有一个强大的类型系统,我用它来代替。

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.

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.

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

蓝天 2024-09-25 00:51:28

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

然而,对于 C 来说,前导下划线不是一个好主意,因此我猜测 C++ FAQ Lite 的建议是尾随下划线:

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

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

<小时>

(ISO C99 规范,第 7.1.3 节)

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

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:

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)

不忘初心 2024-09-25 00:51:28

据我记得,并不是微软为成员推出了尾随下划线代码风格。

我读过 Stroustrup 赞成尾随下划线。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文