Constructors have one purpose - to allow you to guarantee that objects are correctly initialized. The compiler will guarantee that your constructors are called, inserting constructor calls as necessary. This is a great leap in reliability over C programming where, if the programmer forgot to initialize something explicitly, it contained whatever garbage was in memory.
Three types - No-parameter (default), copy constructors, and all others. Only the first two have unexpected aspects.
A x; // Calls default constructor A xa = new A[99]; // Calls default constructor for each array element.
A(const A& a2) { . . . }Note that the parameter must be a const reference.
A x(y); // Where y is of type A. f(x); // A copy constructor is called for value parameters. x = g(); // A copy constructor is called for value returns.
Assume A is a class.
A f(A x) { return x;} A g(A& x) { return x;} A& h(A& x) { return x;}
For each of the following function calls, how many times is the copy constructor called?
A a; A b; a = f(b); a = g(b); a = h(b);