What does `m_` variable prefix mean?

前端 未结 9 735
太阳男子
太阳男子 2020-12-22 18:47

I often see m_ prefix used for variables (m_World,m_Sprites,...) in tutorials, examples and other code mainly related

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-22 19:03

    It is common practice in C++. This is because in C++ you can't have same name for the member function and member variable, and getter functions are often named without "get" prefix.

    class Person
    {
       public:
          std::string name() const;
    
       private:
          std::string name; // This would lead to a compilation error.
          std::string m_name; // OK.
    };
    

    main.cpp:9:19: error: duplicate member 'name'
          std::string name;
                      ^
    main.cpp:6:19: note: previous declaration is here
          std::string name() const;
                      ^
    1 error generated.
    

    http://coliru.stacked-crooked.com/a/f38e7dbb047687ad

    "m_" states for the "member". Prefix "_" is also common.

    You shouldn't use it in programming languages that solve this problem by using different conventions/grammar.

提交回复
热议问题