"Linguagem de programação C ++ (quarta edição)" Capítulo 3.3.2, Capítulo 17.5.2
1. O construtor de movimento é um tipo de construtor, que constrói um objeto movendo os recursos de outros objetos.
#define debug qDebug()<<
struct ceshi
{
int frist;
int second;
ceshi(int one = 0,int two = 0):frist{one},second{two}
{
}
ceshi(ceshi && c):frist{c.frist},second{c.second}
{
debug "调用移动构造";
c.frist = -1;
c.second = -1;
}
};
int main(int argc, char *argv[])
{
ceshi a(4,5);
ceshi b = std::move(a);
debug a.frist << a.second << &a;
debug b.frist << b.second << &b;
}
2 、 std :: move ()
A função desta função é apenas obter a referência rvalue. Embora o nome da função seja chamado move, ela não move nada.
ceshi b = std::move(a);
Escrever dessa maneira chamará o construtor de movimento e passará a referência rvalue obtida como um parâmetro.
3. Se o construtor de movimento não estiver definido na classe, isso chamará o construtor de cópia:
#define debug qDebug()<<
struct ceshi
{
int frist;
int second;
ceshi(int one = 0,int two = 0):frist{one},second{two}
{
}
ceshi(const ceshi & c)
{
this->frist = c.frist;
this->second = c.second;
debug "调用拷贝构造函数";
}
};
int main(int argc, char *argv[])
{
ceshi a(4,5);
ceshi b = std::move(a);
debug a.frist << a.second;
debug b.frist << b.second;
}