I wrote the following code:
#include <iostream>
class A
{
public:
A(){ std::cout << "created" << std::endl; }
A(A& other) { std::cout << "copied" << std::endl; }
A& get(){ std::cout <<"got" << std::endl; return *this; }
~A(){ std::cout << "destroyed" << std::endl; }
};
Now, lines
A a = A().get();
and
A a;
a = A();
compile and work correctly, but
A a = A();
claims:
no matching function for call to ‘A::A(A)’
note: candidates are: A::A(A&)
note: A::A()
And to make things explicit,
A a = (A&)A();
claims:
error: invalid cast of an rvalue expression of type ‘A’ to type ‘A&’
I completely don't understand this behaviour.
P.S. I know, that if I make const reference in copy c_tor, everything will be OK.