![[Dept of Engineering]](http://www.eng.cam.ac.uk/images/house_style/engban-s.gif)
Next: Makefiles
Up: Program Development
Previous: Program Development
It helps if you decide upon a uniform style for writing code.
It's common to suggest that for all
except the most trivial classes it's wise to define
- a void constructor -
- a copy constructor - to create a new object using
values from an existing one. Use call-by-reference to avoid
infinite recursion.
- the assignment operator - that returns a
reference.
And then there's the "the rule of three" -
if you write any one of a copy constructor, copy assignment operator or
destructor, then you will almost certainly need to write all three for your
class to function properly.
Stanley Lippman in Dr.Dobb's Journal, October 99, (p.40) noted that
unnecessary definition of these functions is likely in practise
to make the code bigger and/or slower - he got a 40% speed improvement
by removing all 3 from a piece of code, the compiler's default alternatives
being more efficient.
When deriving classes
- if using public inheritance, most base class member functions
should be declared virtual
to ensure that the derived class customisations will override the base
class behaviour even in a context where a base class object is expected.
- use virtual inheritance if you are not especially concerned about
performance or if you might use multiple inheritance later.
Next: Makefiles
Up: Program Development
Previous: Program Development
Tim Love
2001-07-05