Mar 21, 2009 at 4:15pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
class Rational { // class for rational numbers
public :
Rational(int numerator = 0, int denominator = 1);
~Rational();
...
private :
int n, d; // numerator and denominator
// notice that operator* (incorrectly) returns a reference
friend const Rational& operator *(const Rational& lhs,
const Rational& rhs);
};
// an incorrect implementation of operator*
inline const Rational& operator *(const Rational& lhs,
const Rational& rhs)
{
Rational result(lhs.n * rhs.n, lhs.d * rhs.d);
return result;
}
Rational two = 2;
Rational four = two * two; // same as
// operator*(two, two)
In the "Rational two = 2;" above;
Constructor "Rational(int numerator = 0, int denominator = 1);" which constr gets called?
Thanks.
Last edited on Mar 21, 2009 at 4:53pm UTC
Mar 21, 2009 at 4:51pm UTC
There is only one constructor.
The call that is actually made is
Rational( 2, 1 );
The 1 comes from the default value of the second parameter.
Mar 21, 2009 at 4:56pm UTC
Is it true that if I declare one constructor, I do not get to use ANY of the default constructors provided by the compiler?
If so, then in this case, I do not get to use the default copy constructor. Right?
Last edited on Mar 21, 2009 at 4:58pm UTC
Mar 22, 2009 at 5:10pm UTC
If you declare a constructor, then the compiler does not give you a default constructor. There is only one default constructor -- it is the constructor that takes no parameters. However you still get the default copy constructor.