Complex c{ Complex{1, 2} };
为什么这条语句不会进入移动构造函数呢?
-
用 g++,关闭了优化 g++ -O0 main.cpp
-
用 visual studio 2017,也关闭了优化
#include<iostream>
using std::cout;
using std::endl;
class Complex
{
public:
Complex(double real, double image) : _real(real), _image(image)
{}
Complex(const Complex& right);
Complex(Complex&& right);
Complex& operator=(const Complex& right);
Complex& operator=(Complex&& right);
virtual ~Complex();
private:
double _real {};
double _image {};
};
Complex::Complex(const Complex& right)
{
cout << "&" << endl;
_real = right._real;
_image = right._image;
}
Complex::Complex(Complex&& right)
{
cout << "&&" << endl;
_real = right._real;
_image = right._image;
}
Complex& Complex::operator= (const Complex& right)
{
cout << "= &" << endl;
if (this != &right)
{
_real = right._real;
_image = right._image;
}
return *this;
}
Complex& Complex::operator= (Complex&& right)
{
cout << "= &&" << endl;
if (this != &right)
{
_real = right._real;
_image = right._image;
}
return *this;
}
Complex::~Complex()
{
cout << "des" << endl;
}
int main()
{
Complex c{ Complex{1, 2} };
//Complex c {std::move(Complex{1, 2})};
cout << &c << endl;
return 0;
}