Question about constructor

I am trying to understand the meaning of the following constructor in a class (for creating fractions):

class fract {

private:
int numerator, denominator;

public:
fract(int n = 0, int d = 1) : numerator(n), denominator(d) { }

};

Is the constructor of fraction equivalent to the following?

fract(int n = 0, int d = 1) {numerator=n; denominator=d; reduce(); }

If yes, could anyone give me any reference about this syntax? (I tried to find it, but couldn't)

THANKS!

Manuel
Look up "initializer list".

In this specific case, the two syntaxes you provided are equivalent, but in general use of initializer lists
vs. assignment in the body of the constructor have potential performance ramifications.

EDIT: except for your call to reduce() which is not present in the original version.
Last edited on
Topic archived. No new replies allowed.