实现写时复制
Implementing copy on write
对于一个拥有泛型成员的类,如何做到这一点,比如:
template<typename T> class SP
{
private:
T* data;
reference* ref;
public:
//Some methods here to access data
};
我发现了两种不同的写时复制(COW)方法:
COW Poiner
COWPtr<Object> cow(&obj);
const COWPtr<Object> &cow_ref = cow;
std::cout << cow_ref->name; // operator->() doesn't copy the object because its const overload is used
cow->name = "my object"; // here non const operator->() copies the object
(*cow).name // operator*() also copies the underlying object
来自Adobe stlab 的WRITE方法
COW<Object> cow(&obj);
std::cout << cow->name; // the object is not copied
cow.write().name = "my object"; // the object is copied here
相关文章:
- 为共享 ptr 向量实现复制 c'tor?
- 使用共享指针实现复制 c'tor?
- 如何在双向链表上实现复制赋值?
- 实现不可复制的C++类
- 如何正确实现具有原始指针的类的复制构造函数?
- 复制构造函数和赋值运算符的值类实现
- 如何实现共享数据的复制构造函数
- 在C++中正确实现复制构造函数
- 在一个微不足道的可复制结构中,移动语义应该实现吗?
- 在实现和调用时可以复制函数参数名称吗?
- 如何在模板容器类中实现复制构造函数和赋值运算符
- 在单个链接列表中实现复制构造函数C
- 实现交换/复制功能:有没有更好的方法
- 在 MyVector 中实现写入时复制习语
- 复制列表类的构造函数,而无需在实现中使用任何方法
- 如何为具有自引用指针的类实现复制构造函数/赋值运算符
- 为什么完美的转发(全部捕获)不能实现复制分配?
- 每次复制实现移动分配的非 const 对象时,我是否总是获得移动语义
- 复制在我的字符串中不起作用::复制实现
- “复制”实现示例中的运算符优先级