DIMAJIX - software consulting (http://www.dimajix.de/)
 

Member variables


Rule 37. Initialise all member variables of a class in its constructor.

You should always initialise all member variables of a class, especially pointers should be set to NULL or to an appropriate object. Note that you do not need to explicitly initialise embedded objects, as the C++ compiler implicitly will call their default constructors. Of course this is not true for all simple data types.

This rule ensures that the object is in a well-defined and predictable state after being constructed. Still there is one possible exception to this rule as follows.


Exception to 37. For simple mathematics-related classes you do not need to initialise all member variables.

If working with simple object used for mathematical calculations (like vectors, numbers or simple geometric objects) you can omit the initialisation of their member variables for the sake of speed. As long as they do not contain any pointers (the expression simple class implies this) there is no danger. Think of these classes like of the built-in data types, they also do not provide any default values.


Rule 38. Never specify public or protected member data in a class.

Instead of specifying public or protected data members in your class, you should declare all members as private in most cases and provide getters and setters. This simplifies changes to the internal representation of a class by a magnitude and makes your code more readable. You can even add some parameter checking routines to the setters which help to find bugs. By enabling these checks only in a debug build and by using the inline keyword you do not have to be afraid of any performance penalties. Consider the following example for the correct usage of getters and setters in order to hide the implementation details:

class Animal {
private:
    String          m_Name;
    String          m_Species;
    
public:
    inline const String& getName() const;
    inline void     setName(const String& );
    
    inline const String& getSpecies() const;
    inline void     setSpecies(const String& );
    
    ...
};


Exception to 38. In classes or structs serving as simple containers you may want to make all members public.

This exception makes sense for small classes like a Vector or Rectangle. In these cases using getters and setters does not only produce unnecessary overhead, but also might degrade the readability of your code.

template<class T> class Vector3 {
public:
    T        x,y,z;
	
public:
    ...
};

Note that in this case it is preferable to omit the leading m_ prefix in front of the member variables. This helps further to improve the readability.


Recommendation 39. Either make all members private or all members public. Never mix both cases.

With the last rule and its exception in mind it does not make sense to mix private and public members. Either a class is simple and only has public members or it is complex and only has private members. Note that this also implies that normally it does not make sense to derive from a class with public data members; this would contradict to the required simplicity.

Kaya Memisoglu 2005-01-06