Boost logo

Boost Users :

Subject: Re: [Boost-users] privately inherit enable_shared_from_this?
From: Peter Dimov (pdimov_at_[hidden])
Date: 2009-12-11 13:15:19


Eric J. Holtman wrote:
> I have some code that uses enable_shared_from_this.
>
> I was publicly deriving, i.e. (and this works)
>
> class Request : public boost::enable_shared_from_this<Request> {
> };
>
> I wanted to change my code around so that I wrapped
> the shared_from_this function inside the class, so that
> I could check at runtime whether certain conditions hold,
> i.e.
>
>
> class Request : private boost::enable_shared_from_this<Request> {
> public:
> boost::shared_ptr<Request> get_ptr () {
> if (conditions_are_good)
> return shared_from this ();
> else
> throw std::logic_error ("Bad get");
> };
> };
>
>
> This won't compile (visual studio 2008).

One way to make it work is to add

    friend class boost::enable_shared_from_this<Request>;
    friend class boost::shared_ptr<Request>;

to Request.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net