#include <iostream>
class A{
};
class B: public A{
public:
B(A&& inA){
std::cout<<"constructor"<<std::endl;
}
};
int main(){
B whatever{A{}};
whatever=A{};
}
This outputs
constructor
constructor
at least with C++14 standard and GCC. How is it defined that assignment operator can result in call to constructor instead of operator=
? Is there a name for this property of assignment operator?
Since you meet all the conditions for generating a move-assignment operator. The move-assignment operator the compiler synthesizes for you is in the form of:
B& operator=(B&&) = default;
Recall that temporaries can be bound to const
lvalue references and rvalue references. By Implicit Conversion Sequences, your temporary A{}
is converted to a temporary B
which is used to make the move assignment. You may disable this with explicit
constructors.