4

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:

  1. When one object explicitly initializes another, such as in declaration. E.g. myClass x = y;
  2. 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.
  3. 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?

cigien
  • 57,834
  • 11
  • 73
  • 112
Aditya
  • 397
  • 2
  • 12
  • 2
    To complicate things slightly more, read up on [copy elision](https://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization). – Sander De Dycker Jan 22 '21 at 10:07
  • 2
    You cannot use an object in C++ until it hasn't been initialized. Assignment uses an existing object, which implies, you need to initialize it before. BTW, an assignment to a class object `x` may be sometimes better visualized as `x.operator=(arg)` instead of `x=arg`. This clearly indicates that you are just using an object the same way you would call any other (non-special) member function on it. – Daniel Langr Jan 22 '21 at 10:21

2 Answers2

3

The temporary object needs to be initialized, so that y can be assigned its value.

cigien
  • 57,834
  • 11
  • 73
  • 112
Caleth
  • 52,200
  • 2
  • 44
  • 75
3

When you initialise, it means you give your variable a value for the first time upon creation. If you assign, it means, the object has been created before and now is (re)assigned a value.

std::string name; *A variable is declared but not initialised*
std::string name = 'Sarah'; *The variable is initialised*
name = 'Lisa' *The variable is reassigned a value*

In case 3, the function needs to create a temporary object, which exists only inside the scope of the function. It then returns this, if not defined otherwise, as a value. And the value can be assigned to the existing y, or y is initialized, if it did not have a value before.

You need to keep in mind, that if you want to create new objects from existing objects, you need some sort of copy mechanism. That is why we need a copy constructor:

Person peter = Person{'Peter'} *A new person named peter is created*
Person betterPeter = peter *You initalize newPeter with Peter*

That will only copy the address of peter. So if you make any changes in betterPeter, peter will be affected by the same changes. What you want, is to have a function that copies all elements from peter to betterPeter. This is not so hard: https://en.cppreference.com/w/cpp/language/copy_constructor

cigien
  • 57,834
  • 11
  • 73
  • 112