Reputation: 5146
How can this be done for a class that holds a generic member, something like:
template<typename T> class SP
{
private:
T* data;
reference* ref;
public:
//Some methods here to access data
};
Upvotes: 0
Views: 358
Reputation: 2531
I found two different approaches to copy on write (COW):
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
COW<Object> cow(&obj);
std::cout << cow->name; // the object is not copied
cow.write().name = "my object"; // the object is copied here
Upvotes: 1