拷贝构造
The copy constructors in C++ work with the l-value references and copy semantics(copy semantics means copying the actual data of the object to another object rather than making another object to point the already existing object in the heap). While move constructors work on the r-value references and move semantics(move semantics involves pointing to the already existing object in the memory). On declaring the new object and assigning it with the r-value, firstly a temporary object is created, and then that temporary object is used to assign the values to the object. Due to this the copy constructor is called several times and increases the overhead and decreases the computational power of the code. To avoid this overhead and make the code more efficient we use move constructors.
移动构造
Move constructor moves the resources in the heap, i.e., unlike copy constructors which copy the data of the existing object and assigning it to the new object move constructor just makes the pointer of the declared object to point to the data of temporary object and nulls out the pointer of the temporary objects. Thus, move constructor prevents unnecessarily copying data in the memory. Work of move constructor looks a bit like default member-wise copy constructor but in this case, it nulls out the pointer of the temporary object preventing more than one object to point to same memory location.
左值引用vs右值引用 个人认为i左值和右值的本质区别是=号左右之分,等号右边一般是常量,所以我们右值引用的对象是常量,常量不能更改一定要记住,左值引用和右值引用的字面区别是& ,和&& 之分,比如左值引用int i =20; int l& = i ,右值引用为int && r = 200 ,不能用左值取初始化右值,比如int x = 20;//左值 ,int && r = x //error!!!
总结:拷贝构造就是copy,移动构造是指针指向已经存在的对象,copy构造在构造函数之前会先新建一个临时class obj(在进入构造函数之后,就像我们进入函数之后会将实参赋值给形参),再由这个外部的obj将值赋给新建的class obj内部的这个临时obj后任务就结束了,后续的操作都是这个临时的obj和新的class obj之间的,最后在copy完成后就将这个临时obj析构,这个是非常耗时间,耗内存的,
看下面代码
#include <iostream>
#include <vector>
using namespace std;
class Move{
private:
int* data;
public:
Move(int d){
data = new int;
*data = d;
cout << "constructor is called for " << d << "\n";
}
Move(const Move& source)
: Move{ *source.data }{
cout << "copy constructor is called " << "deep copy for "<< *source.data << "\n";
}
Move(Move&& source)
: data{ source.data }{
cout << "Move Constructor for " << *source.data << endl;
source.data = nullptr;
}
~Move(){
if (data != nullptr)
cout << "Destructor is called for "
<< *data << endl;
else
cout << "Destructor is called"
<< " for nullptr "
<< endl;
delete data;
}
};
int main()
{
vector<Move> vec;
vec.push_back(Move{10});
vec.push_back(Move{20});
return 0;
}
我们先只运行vec.push_back(move{10}) 看打印如下
constructor is called for 10
Move Constructor for 10
Destructor is called for nullptr
Destructor is called for 10
从上面可以看到我们一个简单的push到vector的操作一共构建了2次,第一次是move{10} 构建常量,第二次是在容器内先构造一个临时class(push_back()特点)再将其传进来就像这样Move v1(move{10}); ,所以会调用拷贝构造,然后将这个临时的class传入vector中(此时vector已经构建好空间),然后析构这个临时移动构造的class,最后程序结束析构这个常量
记住move{10}是常量,我们再传入左值发现会调用copy构造,将main函数的代码改成这样vector<Move> vec; Move v1(10) ; vec.push_back(v1); 然后我们可以看到输出
constructor is called for 10
constructor is called for 10
copt constructor is called deep copy for 10
Destructor is called for 10
Destructor is called for 10
最后vec.push_back(Move{10});vec.push_back(Move{20}); 都加上打印如下
constructor is called for 10
Move Constructor for 10
Destructor is called for nullptr
constructor is called for 20
Move Constructor for 20
constructor is called for 10
copt constructor is called deep copy for 10
Destructor is called for 10
Destructor is called for nullptr
Destructor is called for 10
Destructor is called for 20
第一个会先调用构造函数构造常量,再调用拷贝构造,因为传入的是常量
|