gpt4 book ai didi

c++ - 实现写时复制

转载 作者:行者123 更新时间:2023-11-28 08:10:40 25 4
gpt4 key购买 nike

如何为拥有通用成员的类完成此操作,例如:

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

WRITE method from Adobe stlab

COW<Object> cow(&obj);
std::cout << cow->name; // the object is not copied
cow.write().name = "my object"; // the object is copied here

关于c++ - 实现写时复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9171278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com