Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-02-04 08:29:50


Joshua Little wrote:
[...]
>
> So I have something like this:
>
> std::vector<boost::shared_ptr<Base> > BaseVector;
>
> void add(boost::shared_ptr<Base>& newOne) {
> BaseVector.push_back(newOne);
> }
>
> and I call it like this usually :
> boost::shared_ptr<Derived> p(new Derived);
> add(p);
>
> I thought that it would work just like using raw pointers (which was
> working) but I get a compile time error that :
> no matching function call to add(boost::shared_ptr<Derived>&)
> canadites are add(boost::shared_ptr<Base>&).. ect.

It should fail with raw pointers, too, unless your compiler is too
permissive. The problem is that

    void add(boost::shared_ptr<Base>& newOne)

takes a non-const reference to shared_ptr<Base>, so you need a
shared_ptr<Base> to call it. Change it to

    void add(boost::shared_ptr<Base> const & newOne)

and it should work. And, as a general rule, always use a reference to const
when you don't intend to modify the argument.


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