Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-09-21 19:06:20


From: "David Abrahams" <dave_at_[hidden]>
> All the examples I've been able to come up with where I want a weak_ptr
> also lead naturally to wanting shared_from_this()... well, actually,
> weak_from_this(). The basic paradigm is that a member function is used to
> add owned objects which need a back-pointer. Inside a member function you
> have a raw pointer, not a shared_ptr, regrettably.
>
> Is there an example which maps naturally onto non-intrusive use of
> weak_ptr?

std::vector< shared_ptr<GameObject> > objects;

class Tank: public GameObject
{
    weak_ptr<GameObject> target_;

public:

    virtual void update()
    {
        if(shared_ptr<GameObject> p = make_shared(target_))
        {
            this->fireAt(p);
        }
        else
        {
            target_ = this->acquireNewTarget(objects);
        }
    }
};

The basic paradigm would be siblings that refer to each other.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk