home > resources > c++ coding standard > online version

Chapter 4. Class Design

Required Class Methods

To be good citizens almost all classes should implement the following methods. If you don't have to define and implement any of the "required" methods they should still be represented in your class definition as comments. If you just let the compiler generate them without indicating through comments that you know that this is the intended behaviour, people might wonder about the possibility of an ommision or oversight.

Details

Default Constructor

If your class needs a constructor, make sure to provide one. You need one if during the operation of the class it creates something or does something that needs to be undone when the object dies. This includes creating memory, opening file descriptors, opening transactions etc.

If the default constructor is sufficient add a comment indicating that the compiler-generated version will be used.

If your default constructor has one or more optional arguments, add a comment indicating that it still functions as the default constructor.

Virtual Destructor

If your class is intended to be derived from by other classes then make the destructor virtual. You should always make a destructor virtual for the sake of future extensibility. Only make it non virtual if you've got a real good reason to do so.

Copy Constructor

If your class is copyable, either define a copy constructor and assignment operator or add a comment indicating that the compiler-generated versions will be used.

If your class objects should not be copied, make the copy constructor and assignment operator private and don't define bodies for them. If you don't know whether the class objects should be copyable, then assume not until the copy operations are needed.

Assignment Operator

If your class is assignable, either define a assignment operator or add a comment indicating that the compiler-generated versions will be used.

If your objects should not be assigned, make the assignment operator private and don't define bodies for them. If you don't know whether the class objects should be assignable, then assume not.

Justification

  • Virtual destructors ensure objects will be completely destructed regardless of inheritance depth. You don't have to use a virtual destructor when:
    • You don't expect a class to have descendants.
    • The overhead of virtualness would be too much.
    • An object must have a certain data layout and size.
  • A default constructor allows an object to be used in an array.
  • The copy constructor and assignment operator ensure an object is always properly constructed. Making them private, prevents copies from objects being made without you knowing about it and thus possibly inducing an unnecessary overhead or cause for inconsistency.

Example 4-1. Required Class Methods Example

class Planet
{
public:
    // Planet();
    Planet(int radius= 5);
    ~Planet();
    
private:
    Planet(const Planet&);
    Planet& operator=(const Planet&);
};