Programming is like playing the
piano --
you must practice just to maintain your capabilities
C++老师的教诲
Pass parameters by const references rather than by value.
Pass by value will not only cause more stack memory to be used up, it will
also cost the running of a constructor just before the call and a destructor
just after the call.
Use the constructor's member initialization list rather than assignment
within the constructor.
The member's constructors are always called when an object is being
constructed. The member initialization list just modifies the member's
constructor whereas the assignment just overwrites the constructor's work.
Learn to use <iostream.h> rather than <stdio.h>.
<iostream.h> is type safe and it can be extended to work with new classes.
<stdio.h> is as modern as FORTRAN, anything other than type safe and it only
handles the basic types. It is time to move on.
If you have a class Z that holds a pointer to another object, then Z
should have a coy constructor, an overloaded assignment operator and a
destructor.
You should do deep copies rather than the default shallow copies. When an
object holds a pointer to another class, it is responsible for its
destruction.
Use const whenever possible.
Liberal use of const reduces errors and prevents errors in the future. It
might be painful in the very beginning, but with good design const helps keep
a consistent implementation.
Do not make class data public! Never do this! Use inline public accesses
to protected or private data instead. Never provide pointers or references to
an objects protected data.
A class' data is its business, there should be no other software involved
in data.
Assignment operators must return lvalues - use a reference to return
*this.
Remember that A = B = C; is a valid statement. This implies that
assignments must return values that could be used elsewhere in an expression.
Remember A = A; is a valid statement, watch out for this when overloading
the assignment operator.
Non-assignment operators must create an object - as the return value.
Do not return a reference to an object, return the object itself.
Programming is like playing the piano -- you must practice just to
maintain your capability.
If your work doesn't have you programming in C++, assign yourself a
project and spend at least 2 hours per week to keep your skills.