Trailing underscores for member variables in C++

前端 未结 7 411
后悔当初
后悔当初 2020-12-13 03:46

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 var

相关标签:
7条回答
  • 2020-12-13 04:43

    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.

    0 讨论(0)
提交回复
热议问题