Warren,
Looks all right - haven't tested it.
Slight improvements might be made by only testing counted_ to know if
it is a non null pointer instead of checking ref_ is set all the time.
Like below.
Untested - but if it doesn't work you get the idea.
As mentioned by others it is probably not exception safe in assign(T
* c) this is best handled using the .swap idiom that is in boost
pointers and standard library.
Alex
void assign(T* c)
{
detach();
counted_ = c;
if (c) {
refs_ = new size_t(1);
}
}
void assign(const ThisType& other)
{
detach();
counted_ = other.counted_
if (counted_ ) {
refs_ = other.refs_;
++(*refs_);
}
}
void detach()
{
if (counted_)
if (--(*refs_) == 0))
{
delete counted_;
delete refs_;
}
}