Boost logo

Boost Users :

From: Delfin Rojas (drojas_at_[hidden])
Date: 2006-07-14 13:28:42


> Zhuo Hao wrote:
>
> In file <boost/shared_ptr.hpp>, I find the following function:
> /begin quote
> template<class Y, class D> shared_ptr(Y * p, D d): px(p), pn(p, d)
> {
> detail::sp_enable_shared_from_this( pn, p, p );
> }
> /end quote
> If I use it as follows:"boost::shared_ptr<Investment>
> pInv(0,getRidOfInvestment)",it will not compile.But when I
> use:"boost::shared_ptr<Investment>
> pInv(static_cast<Investment*>(0),getRidOfInvestment)",it can
> be compiled.
> The constructor's first parameter is Y*,i.e.,a pointer to
> Y.And 0 can be interpreted as a pointer to Y,too. Why can't
> zero be treated as a pointer to Y here?
>

Y (the type of the object passed as the first parameter of the constructor)
may not be the same as T (the type of the shared pointer). In other words I
can have shared_ptr<Investment> p(new SpecificInvestment(),
getRidOfInvestment); as long as class SpecificInvestment inherits from
Investment.

So, when you use boost::shared_ptr<Investment> pInv(0,getRidOfInvestment)
the compiler doesn't know 0 is of type Investment*.

Hope this helps,

-delfin


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