Coding Guidelines
C++ Guidelines
Class Specification
<- ^ ->
<< ^^ >>


Member Declaration

Use access control Whenever possible limit access to member data and functions.
Prefix data members with "m_" Class data members should be prefixed with "m_". The variable name following the "m_" should follow the normal variable naming conventions.
Prefix static data members with "g_" Class data members that are static (shared between all occurrences of the class) should be prefixed with "g_". The variable name following the "g_" should follow the normal variable naming conventions.
Order class members Declare class members with the most public member access before the more limited access members. The easiest way to do this is to use the file-templates when creating new files.
  • Class life-cycle (constructors, destructors, initialization)
  • Public
    • Types
    • Functions
  • Protected
    • Types
    • Functions
    • Data
  • Private
    • Types
    • Functions
    • Data
  • Overrides

Constructors

Provide copy constructor for classes that allocate memory Any class that allocates memory or holds onto system resources should always provide a copy constructor.
Disable copy operations by moving the copy constructor to a protected label If your class should not use block copy methods for copying your data, then you can disable the copy by declaring the copy constructor as protected.

Destructors

Use destructors to cleanup Here are the rules about when your class needs a destructor.
  • Your class allocates memory and needs to delete it.
  • Your class attaches to system resources and needs to release/close them
  • Your class AddRefs other COM objects and needs to Release them.
Declare destructors as virtual if any member is virtual There are two primary rules about when a class's destructor should be declared as virtual.
  • If your class (or any of its parents) has any virtual functions.
  • If your class can be used as a base class. And that the derived class may be deleted through a pointer of your base class's type.

<- ^ ->
<< ^^ >>
Last modified: 25 May 2009
copyright 2004 Bear Consulting Group