I was going through the book: "C++: The Complete Reference", 4e. In the topic of copy constructors, the following is written:
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 an assignment. The second is an initialization, which can occur in any of the three ways:
- When one object explicitly initializes another, such as in declaration. E.g. myClass x = y;
- When a copy of an object is made to be passed to a function. E.g. func(y); // where y is an object of some class.
- When a temporary object is generated (most commonly, as a return value). E.g. y = func(); // y receiving a temporary, return object.
I understand the 1st and 2nd cases, but not the 3rd one.
In the 3rd case, if the function returns a temporary object why wouldn't an assignment operation happen between y and the temporary object?
What is the necessity for an initialization?
If at all initialization is happening, where is it happening? I mean, what is getting initialized?