What does `m_` variable prefix mean?

前端 未结 9 698
太阳男子
太阳男子 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:15

    Lockheed Martin uses a 3-prefix naming scheme which was wonderful to work with, especially when reading others' code.

       Scope          Reference Type(*Case-by-Case)   Type
    
       member   m     pointer p                       integer n
       argument a     reference r                     short   n
       local    l                                     float   f
                                                      double  f
                                                      boolean b
    

    So...

    int A::methodCall(float af_Argument1, int* apn_Arg2)
    {
        lpn_Temp = apn_Arg2;
        mpf_Oops = lpn_Temp;  // Here I can see I made a mistake, I should not assign an int* to a float*
    }
    

    Take it for what's it worth.

    0 讨论(0)
  • 2020-12-22 19:17

    This is typical programming practice for defining variables that are member variables. So when you're using them later, you don't need to see where they're defined to know their scope. This is also great if you already know the scope and you're using something like intelliSense, you can start with m_ and a list of all your member variables are shown. Part of Hungarian notation, see the part about scope in the examples here.

    0 讨论(0)
  • 2020-12-22 19:23

    As stated in many other responses, m_ is a prefix that denotes member variables. It is/was commonly used in the C++ world and propagated to other languages too, including Java.

    In a modern IDE it is completely redundant as the syntax highlighting makes it evident which variables are local and which ones are members. However, by the time syntax highlighting appeared in the late 90s, the convention had been around for many years and was firmly set (at least in the C++ world).

    I do not know which tutorials you are referring to, but I will guess that they are using the convention due to one of two factors:

    • They are C++ tutorials, written by people used to the m_ convention, and/or...
    • They write code in plain (monospaced) text, without syntax highlighting, so the m_ convention is useful to make the examples clearer.
    0 讨论(0)
提交回复
热议问题