C ++中的成员变量的下划线下划线 [英] Trailing underscores for member variables in C++

查看:514
本文介绍了C ++中的成员变量的下划线下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到人们在类中为成员变量使用尾随下划线,例如着名的 C ++ FAQ Lite



我认为它的目的不是将变量标记为成员,这就是m_。它的实际目的是使它有可能有一个类似于字段的访问器方法,如下:

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

存取器省略get_部分在STL和boost中是常见的,我试图开发一个尽可能接近这些的编码风格,但我不能真正地看到他们使用下划线的伎俩。我不能在STL中找到一个访问器或boost,只返回一个私有变量。



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


  1. 这个约定来自哪里?短暂聊天? Objective-C?微软?

  2. 我要为所有私人成员使用尾随下划线,或者只是作为变通方法,以便我命名一个像变量这样的函数?

  3. 你能指定STL或提升代码来显示成员变量的尾随下划线吗?

  4. 有人知道Stroustrup对这个问题的看法吗?
  5. $ b $


    1. 标识符以下划线开头,后跟大写字符

    2. 标识符具有两个连续的下划线

    的全局命名空间中的

  6. 标识符保留给实现。 (有关详情,请访问此处。)除了尝试记住这些规则,很多人根本不使用以下划线。这就是为什么发明尾随下划线的原因。
    然而,C ++本身是老的,建立在40年的C上,两者都没有一个公司在后面,一个标准库已成长几十年来,而不是在一个单一的创作行为。这使得存在很多不同的命名约定。后缀下划线为私有(或只有私人数据)只是一个,许多使用其他(在他们中的少数人认为,如果你需要下划线告诉私人成员从局部变量,你的代码不够清楚)。



    对于getters / setters - 它们是可憎的,并且是准类,我讨厌。


    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?

    解决方案

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

    这篇关于C ++中的成员变量的下划线下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆