If the default constructor is not needed, hide it in the private part in order to avoid misuse.
For example:
class A
{
public:
A( const int input_id ) { id = input_id; }
int getID() const { return id; }
private:
double id;
}
if this class has to be always created with the defined constructor,
put the default one in the private part like
class A
{
public:
A( const int input_id ) { id = input_id; }
int getID() const { return id; }
private:
double id;
A();
}
Then other users will never get a chance to do the following:
A a; //there will be a compiling error for this
Note that the compiler will define a default constructor if it is not defined
by the user.
Remember that debugging is expensive.
[ 此帖被steinlee在2010-02-25 06:30重新编辑 ]