Given class:
class C
{
public:
C()
{
cout << "Dflt ctor.";
}
C(C& obj)
{
cout << "Copy ctor.";
}
C(C&& obj)
{
cout << "Move ctor.";
}
C& operator=(C& obj)
{
cout << "operator=";
return obj;
}
C& operator=(C&& obj)
{
cout << "Move operator=";
return obj;
}
};
and then in main:
int main(int argc, char* argv[])
{
C c;
C d = c;
C e;
e = c;
return 0;
}
as you will see from the output the “regular” version of copy ctor and operator= are invoked but not those with rvalue args. So I would like to ask in what circumstances will the move ctor and operator=(C&&) be invoked?
The move constructor will be invoked when the right-hand side is a temporary, or something that has been explicitly cast to
C&&either usingstatic_cast<C&&>orstd::move.