Thread: Variable naming

  1. #1
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51

    Variable naming

    Hi. I've seen people naming their variables as m_*. What does the m_ mean? And why do you use it?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It usually indicates that this is a (class) member variable.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As explained, m_ (or m) means "member" variable for classes, assuming it actually means anything.

    There are MANY ways to name variables, and the "where does this belong" naming convention is just one of those. It does make sense to indicate that a variable is part of a class, to differentiate it from for example function parameters or local variables in the function.

    Where I work, arguments to functions are prefixed with a, so you can tell the difference between an argument and other local variables too.

    Many people also prefix globals with g or g_.

    Other naming conventions indicate what the type of a variable is, and I've seen prefixing of the type "ui32" to indicate that it's a "unsigned integer" that is 32 bits long. I don't find that particularly helpful most of the time (especially when you have an editor that allows you to jump to the declaration/definition of a variable easily), but I can see the logic.

    Whether you use a prefix naming convention or not, make sure that you follow a consistent system - there's nothing worse than looking at code that has three different naming conventions in the same project. You never really know what you are looking at...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    39
    It is nOthing, JUst a Microsoft Naming Convention..
    m_ = Member variable of class,
    _ = Public variable of class ...
    So on....

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I do this:

    Code:
    class Foo
    {
    private:
       int m_data; // data member of a class
       
       int *pm_data; // data member of a class that is also a pointer
    };
    As pointed out, it is all preference rather than essentials. But like matsp said, be consistant.
    Double Helix STL

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by obaid View Post
    It is nOthing, JUst a Microsoft Naming Convention..
    m_ = Member variable of class,
    _ = Public variable of class ...
    So on....
    I wouldn't call it a Microsoft naming convention, since it's pretty common all over.
    Prefixes like lpsz or dw are a lot more Microsoftish though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  2. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  3. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  4. newb question~
    By Anima in forum C++ Programming
    Replies: 10
    Last Post: 08-24-2003, 05:54 PM
  5. Naming variables, functions...
    By Ariod in forum Tech Board
    Replies: 9
    Last Post: 08-19-2003, 12:17 PM