Constructor Functions  «Prev 

Initialization versus Assignment in C++

In constructors, initialization is preferred to assignment.
For example:

ch_stack::ch_stack(int size){
   s = new char[size];
   max_len = size;
   top = EMPTY;

is better written as
ch_stack::ch_stack(int size):
   max_len(size), top(EMPTY)
   { s = new char[size]; }

Data members that are reference declarations or const declarations must be initialized.
Also, the compiler can often be more efficient about initialization.

Before we continue, it is important to understand that C++ defines two distinct types of situations in which the value of one object is given to another.
The first is assignment and the second is initialization, which can occur in three ways:
  1. When one object explicitly initializes another, such as in a declaration,
  2. When a copy of an object is made to be passed to a function, or
  3. When a temporary object is generated (most commonly, as a return value).

The copy constructor applies only to initializations. The copy constructor does not apply to assignments. The most common general form of a copy constructor is shown here:
classname (const classname &obj) {
// Body of copy constructor.

Here, obj is a reference to the object on the right side of the initialization. It is permissible for a copy constructor to have additional parameters as long as they have default arguments defined for them. However, in all cases, the first parameter must be a reference to the object doing the initializing. This reference can also be const and/or volatile. Again, assume a class called myclass and an object of type myclass called A. Also assuming that func1( ) takes a myclass parameter and that func2( ) returns a myclass object, each of the following statements involves initialization:
myclass B = A; // A initializing B
myclass B(A); // A initializing B
func1(A); // A passed as a parameter
A = func2(); // A receiving a temporary, return object

In the first three cases, a reference to A is passed to the copy constructor.
In the fourth, a reference to the object returned by func2( ) is passed to the copy constructor.Inside a copy constructor, you must manually handle the duplication of every field within the object. This, of course, gives you a chance to avoid potentially harmful situations. For example, in myclass just described, the new myclass object could allocate its own memory. This would allow both the original and the copy to be equivalent but fully separate objects. It also avoids the problem of both objects using the same memory because freeing one object's memory would not affect the other.
If necessary, the memory could be initialized to contain the same contents as the original. In some cases, the same problems that can occur when making a copy of an object also occur when one object is assigned to another. The reason is that the default assignment operator makes a member-by-member, identical copy. You can avoid problems by overloading operator=( ) so that you handle the assignment process yourself.