Description of intent with unique_ptr

Is the following a way to describe ownership intentions (without using shared_ptr?)

class Z { };
class A
{
    unique_ptr<Z> m_z; //want to say 'I own Z'
};

class B
{
    B(A & a) 
    { 
        m_z = a._z.get(); 
    }

    Z* m_z; //want to say 'I do not own Z, just a ref...'
}

In addition, B._z may hang. Is there a way to fix the problem without resorting to shared_ptr and weak_ptr?

+3
source share
1 answer

unique_ptr is normal for the owner. If the original pointers to this object are still issued and stored elsewhere (and therefore they are technically separable), this may confuse readers of your code.

shared_ptr , . , , B , _z . , shared_ptr weak_ptr B. ... Boost.Signals2.

, weak_ptr , .

+2

Source: https://habr.com/ru/post/1780979/


All Articles