一、前置知识
二、为什么需要std::move?
想象一下:有一个类型A,它有一个需要动态申请资源的数据成员i_ptr。接着,你会调用函数wrapper来往A类型的数组vec中添加元素。
#include <cstddef>
#include <iostream>
#include <vector>
class A {
public:
A(int i)
{
// std::cout << __PRETTY_FUNCTION__ << std::endl;
i_ptr = new int(i);
// std::cout << "allocate memory addr: " << i_ptr << std::endl;
}
~A()
{
// std::cout << __PRETTY_FUNCTION__ << std::endl;
if (i_ptr) {
delete i_ptr;
// std::cout << "deallocate memory addr: " << i_ptr << std::endl;
i_ptr = nullptr;
}
}
A(const A &obj)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
i_ptr = new int(*obj.i_ptr);
// std::cout << "allocate memory addr: " << i_ptr << std::endl;
}
A(A &&obj)

最低0.47元/天 解锁文章
941

被折叠的 条评论
为什么被折叠?



