Salt_Peter said:
class Control {
Point origin;
public:
Control();
Control(int x, int y);
};
Control::Control() : origin(0, 0) { } // or origin( ) if Point itself
has a default ctor
Control::Control(int x, int y) : origin(x, y) { } // invokes
Point(int,int)
And there is yet another way to do all of the above:
class Control {
Point origin;
public:
Control(int x = 0, int y = 0);
};
That's not equivalent to your first example. As well as allowing these
two forms of construction
Control c1;
Control c2(42, 42);
the default parameters allow this
Control c3(1); // x is 1, y is zero.
which may well not be what is wanted. Moreover, because the single
argument constructor is not explicit, this is also allowed
Control c4(1, 1);
c4 = 42;
c4 = '\n';
which is very likely not what is wanted.
If it only makes sense to construct a Control with zero arguments or
with two arguments, give it two constructors (as in your first
example). If it does make sense to construct with only one argument
then the default parameters are OK. For me, the opportunity to save
some typing at the point of construction is not enough of a reason. I
would only use the default parameters if y == 0 really was a sensible
default behaviour and it was an unusual special case for y to need a
different value. If you choose that route, declare the constructor
explicit in the class unless it really does make sense to be able to
implicitly convert from, for example, a char to a Control.
class Control {
public:
explicit Control(int x = 0, int y = 0) {}
};
int main()
{
Control c4(1, 1);
c4 = 42; // Not allowed with explicit constructor
c4 = '\n'; // Not allowed with explicit constructor
}
Gavin Deane