initialization question

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
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.
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
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.
Topic archived. No new replies allowed.