Hi. It looks like we have a lot of new developers starting up, so I thought I would very quickly outline some of the standard naming conventions. I've seen source where people did not understand these conventions, and it made the source terribly confusing. I also have never seen any guide that consolidated all these naming conventions, so I thought I would whip one up.
Most of this is related to MFC, but you should still use these conventions for general C++. The main idea of these naming conventions is to determine information about a variable or type without having to consult the header file it was defined in.
Classes
Class names should start with a capital C, ie:
CMyClass, CAWindow, CCustomButton
Note that the header and source file names do not start with C:
MyClass.cpp, MyClass.h, AWindow.h, etc.
Also note that when you create a new class with ClassWizard, if the class name starts with C it will strip that C off of the filenames. So if you do not use the above convention, and try to create a new class such as "Car", ClassWizard will use the filenames "ar.h" and "ar.cpp" by default.
Variables
Usually variable names, particularly members of a Class, are preceded by lower case letters to indicate what type the variable has. This is called Hungarian Notation. For example:
You can see that p indicates a pointer, n = integer, sz = string (the z means it is zero terminated), h = Windows Handle, etc.
Hungarian Notation is a little less useful when you have powerful development GUIs, like eVC++ and VC++, where you can just hover your mouse over a variable name to see how it is defined. You can imagine the old days when all code was written in a simple text editor. Hungarian Notation saved a great deal of time in those days (and still can).
Class Variable Members
One of the most important conventions is the naming of a Class' member variables. These should be preceeded by m_ ie:
This is important for a few reasons. Primarily it gives you greater freedom in your member functions so you don't have to worry that you create a variable that is already a class member. Imagine if you have a class that is derived from several others, and one of the base classes has a member variable "i". If you used a local i in some functions, then it wouldn't always be clear which i was being referred to. Conversely, never prefix a local variable with m_! I downloaded some source once where the author was obviously unclear what the m_ represented, and created some local variables with that convention. It resulted in a confusing mess.
Well, those are the biggies. I try to use the Hungarian Notation when I can, but I still slip from time to time. Certainly the most important is to precede class member variables with "m_"!
Dan East
Most of this is related to MFC, but you should still use these conventions for general C++. The main idea of these naming conventions is to determine information about a variable or type without having to consult the header file it was defined in.
Classes
Class names should start with a capital C, ie:
CMyClass, CAWindow, CCustomButton
Note that the header and source file names do not start with C:
MyClass.cpp, MyClass.h, AWindow.h, etc.
Also note that when you create a new class with ClassWizard, if the class name starts with C it will strip that C off of the filenames. So if you do not use the above convention, and try to create a new class such as "Car", ClassWizard will use the filenames "ar.h" and "ar.cpp" by default.
Variables
Usually variable names, particularly members of a Class, are preceded by lower case letters to indicate what type the variable has. This is called Hungarian Notation. For example:
- Code:
int nCount;
LPTSTR pszName
TCHAR szAddress[256];
int *pnTotal;
HANDLE hWnd;
HDC hDC;
You can see that p indicates a pointer, n = integer, sz = string (the z means it is zero terminated), h = Windows Handle, etc.
Hungarian Notation is a little less useful when you have powerful development GUIs, like eVC++ and VC++, where you can just hover your mouse over a variable name to see how it is defined. You can imagine the old days when all code was written in a simple text editor. Hungarian Notation saved a great deal of time in those days (and still can).
Class Variable Members
One of the most important conventions is the naming of a Class' member variables. These should be preceeded by m_ ie:
- Code:
int m_nCounter;
LPTSTR m_pszFirstName;
This is important for a few reasons. Primarily it gives you greater freedom in your member functions so you don't have to worry that you create a variable that is already a class member. Imagine if you have a class that is derived from several others, and one of the base classes has a member variable "i". If you used a local i in some functions, then it wouldn't always be clear which i was being referred to. Conversely, never prefix a local variable with m_! I downloaded some source once where the author was obviously unclear what the m_ represented, and created some local variables with that convention. It resulted in a confusing mess.
Well, those are the biggies. I try to use the Hungarian Notation when I can, but I still slip from time to time. Certainly the most important is to precede class member variables with "m_"!
Dan East