class - C++ constructor not called -


In the following code the manufacturer is called only once (ie) when the car () is executed. Why is it not called a car O1 (car ()) for the second time?

  #include & lt; Stdio.h & gt; # Include & lt; Iostream & gt; Class car {public: car () {std :: cout & lt; & Lt; "Constructor" & lt; & Lt; '\ N'; } Car (car & obj) {std :: cout & lt; & Lt; "Copy Constructor" & lt; & Lt; '\ N'; }}; Int main () {car (); Car O1 (car ()); // do not call any manufacturer 0 calling; }  

  car o1 (car ());   

Announces a function called o1 which gives a car and takes a single logic that is a The returning car is a function . It is known as.

You can fix this by using an extra pair of parentheses:

  car o1 ((car ()); Using uniform initialization in  

or C ++ 11 and beyond:

  car o1 {car {}};  

But for working on it, you should get a car constructor one const car & amp; You will need to create a parameter type of , otherwise you will not be able to temporarily bind it.


Comments