Why do I need const copy constructor when compiling my code with converting constructor?

  • A+
Category:Languages

I have such class in A.h :

class A { public:     A(int *object) {         std::cout << "convert";     }      A(A &object) {         std::cout << "copy";     } }; 

and in main.cpp

A a = new int; 

then, when I'm trying to compile it i get

invalid initialization of non-const reference of type ‘A&’ from an rvalue of type ‘A’

but when i add const to copy-constructor like that:

A(const A &object) {     std::cout << "copy"; } 

code compiles and "convert" is called. It works when i remove copy constructor, too. Why such thing happen? I though this example has nothing to do with copying constructor as we do not use instance of class A to create another.


Because the way your code works is following (pre-C++17):

A a(A(new int)); 

Note, copy constructor of A is called with a temporary object crated as a result of A(new int). And you can't bind a non-const lvalue reference to a temporary.

Just a note, in C++17 this code will compile due to guaranteed copy-elision (in case of C++17, this code is semantically equivalent to A a(new int). You also won't see copy printed (because it will never be called due to copy elision)

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: