When is the C ++ constructor not being called?

I have a situation where the constructor is not being called:

#include <iostream> using namespace std; int main () { class yoyo { public: int i; yoyo() { i = 0; cout << "defaultly initialized to 0" << endl; } yoyo (int j) : i(j) { cout << "initialized to " << j << endl; } }; int i; yoyo a; cout << "Hello1, i: " << ai << endl; yoyo b(5); cout << "Hello2, i: " << bi << endl; yoyo c = b; /* 1 */ cout << "Hello3, i: " << ci << endl; return 0; } 

Output:

 defaultly initialized to 0 Hello1, i: 0 initialized to 5 Hello2, i: 5 Hello3, i: 5 

(Note: there was nothing between Hello2 and Hello3)

If I changed the program as follows:

 #include <iostream> using namespace std; int main () { class yoyo { public: int i; yoyo() { i = 0; cout << "defaultly initialized to 0" << endl; } yoyo (int j) : i(j) { cout << "initialized to " << j << endl; } }; int i; yoyo a; cout << "Hello1, i: " << ai << endl; yoyo b(5); cout << "Hello2, i: " << bi << endl; yoyo c; c = b; /* 1 */ cout << "Hello3, i: " << ci << endl; return 0; } 

(The only difference is in the line marked with the symbol / * 1 * /)

Now the conclusion:

 defaultly initialized to 0 Hello1, i: 0 initialized to 5 Hello2, i: 5 defaultly initialized to 0 Hello3, i: 5 

Now between Hello2 and Hello3 there is a constructor call. My question is why in the first case there is no (visible) constructor call?

+6
source share
4 answers

When

 yoyo c = b; 

created copy constructor .

And in case

 yoyo c; c = b; 

This is the called copy assign statement.

If you do not provide any of them, the compiler will generate default versions for you.


If you want to create your own copy constructor, it might look like this:

 yoyo(const yoyo& other) : i(other.i) { std::cout << "copy constructor initialized\n"; } 

The copy assignment operator is as follows:

 yoyo& operator=(const yoyo& other) { i = other.i; return *this; } 

Both are defined inside the class definition, of course.

+16
source

In the first case:

 yoyo c = b; 

calls the copy constructor , which the compiler generates for you implicitly in this case.

+7
source
  yoyo c = b; 

This is called copy initialization; the instance constructor created by the compiler is called, and c will be initialized with this copy. In addition, the default constructor c will be called.

 c = b; 

Here it is not an initialization, it is an appointment. On this line, the assignment operator created by the compiler will be called.

+3
source

in your code

yoyo c=b will call the copy constructor. If you want it to be called, you must define it explicitly.

eg:

 yoyo(const yoyo& obj) { this->i=obj.i; cout<<"copy constructor"<<endl; } 

in the second case, it will call the constructor, and then the assignment operator

 yoyo c; //constructor c = b; //assignment operator for which only copying occurs 

you can overload the assignment operator as shown below

 yoyo& operator=(yoyo& obj) { i = obj.i; cout << "assignment operator" << endl; } 
+1
source

Source: https://habr.com/ru/post/948029/


All Articles