Why use prefixes on member variables in C++ classes

前端 未结 29 1198
半阙折子戏
半阙折子戏 2020-11-28 17:39

A lot of C++ code uses syntactical conventions for marking up member variables. Common examples include

  • m_memberName for public members (where public
相关标签:
29条回答
  • 2020-11-28 17:40

    Those conventions are just that. Most shops use code conventions to ease code readability so anyone can easily look at a piece of code and quickly decipher between things such as public and private members.

    0 讨论(0)
  • 2020-11-28 17:43

    I generally don't use a prefix for member variables.

    I used to use a m prefix, until someone pointed out that "C++ already has a standard prefix for member access: this->.

    So that's what I use now. That is, when there is ambiguity, I add the this-> prefix, but usually, no ambiguity exists, and I can just refer directly to the variable name.

    To me, that's the best of both worlds. I have a prefix I can use when I need it, and I'm free to leave it out whenever possible.

    Of course, the obvious counter to this is "yes, but then you can't see at a glance whether a variable is a class member or not".

    To which I say "so what? If you need to know that, your class probably has too much state. Or the function is too big and complicated".

    In practice, I've found that this works extremely well. As an added bonus it allows me to promote a local variable to a class member (or the other way around) easily, without having to rename it.

    And best of all, it is consistent! I don't have to do anything special or remember any conventions to maintain consistency.


    By the way, you shouldn't use leading underscores for your class members. You get uncomfortably close to names that are reserved by the implementation.

    The standard reserves all names starting with double underscore or underscore followed by capital letter. It also reserves all names starting with a single underscore in the global namespace.

    So a class member with a leading underscore followed by a lower-case letter is legal, but sooner or late you're going to do the same to an identifier starting with upper-case, or otherwise break one of the above rules.

    So it's easier to just avoid leading underscores. Use a postfix underscore, or a m_ or just m prefix if you want to encode scope in the variable name.

    0 讨论(0)
  • 2020-11-28 17:43

    Other languages will use coding conventions, they just tend to be different. C# for example has probably two different styles that people tend to use, either one of the C++ methods (_variable, mVariable or other prefix such as Hungarian notation), or what I refer to as the StyleCop method.

    private int privateMember;
    public int PublicMember;
    
    public int Function(int parameter)
    {
      // StyleCop enforces using this. for class members.
      this.privateMember = parameter;
    }
    

    In the end, it becomes what people know, and what looks best. I personally think code is more readable without Hungarian notation, but it can become easier to find a variable with intellisense for example if the Hungarian notation is attached.

    In my example above, you don't need an m prefix for member variables because prefixing your usage with this. indicates the same thing in a compiler-enforced method.

    This doesn't necessarily mean the other methods are bad, people stick to what works.

    0 讨论(0)
  • 2020-11-28 17:44

    You should never need such a prefix. If such a prefix offers you any advantage, your coding style in general needs fixing, and it's not the prefix that's keeping your code from being clear. Typical bad variable names include "other" or "2". You do not fix that with requiring it to be mOther, you fix it by getting the developer to think about what that variable is doing there in the context of that function. Perhaps he meant remoteSide, or newValue, or secondTestListener or something in that scope.

    It's an effective anachronism that's still propagated too far. Stop prefixing your variables and give them proper names whose clarity reflects how long they're used. Up to 5 lines you could call it "i" without confusion; beyond 50 lines you need a pretty long name.

    0 讨论(0)
  • 2020-11-28 17:45

    I prefer postfix underscores, like such:

    class Foo
    {
       private:
          int bar_;
    
       public:
          int bar() { return bar_; }
    };
    
    0 讨论(0)
  • 2020-11-28 17:45

    I use it because VC++'s Intellisense can't tell when to show private members when accessing out of the class. The only indication is a little "lock" symbol on the field icon in the Intellisense list. It just makes it easier to identify private members(fields) easier. Also a habit from C# to be honest.

    class Person {
       std::string m_Name;
    public:
       std::string Name() { return m_Name; }
       void SetName(std::string name) { m_Name = name; }
    };
    
    int main() {
      Person *p = new Person();
      p->Name(); // valid
      p->m_Name; // invalid, compiler throws error. but intellisense doesn't know this..
      return 1;
    }
    
    0 讨论(0)
提交回复
热议问题