I've been using shared pointers for a few months now and just discovered
I have been using them the wrong way, causing massive memory leaks. So
I'm going back through my code and cleaning everything up and making the
changes to correct all the mistakes I had been making. So now I've run
into a bit of a problem. I'm using Boost 1.30.2 currently and I have
function that takes in a Base class to place into a vector, but I have a
number of Derived classes that will be going into the vector as well.
From reading through the docs and the mailing lists everything seems to
indicate that the conversion should be implicit and there shouldn't be a
problem.
I have the feeling I am missing something here so I was wondering if
someone could point out my error.
So I have something like this:
std::vector 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.
So I think I'm doing this the wrong way, about the only thing I can
think of was something I saw in a few postings.
Essentially creating a new pointer with the derived pointer then passing
it into the function like this :
boost::shared_ptr<Derived> p(new Derived);
boost::shared_ptr<Base> q(p);
add(q);
Is that the preferred way of doing it? Or is there another step I'm
missing when creating the shared_ptrs to allow them to be
implicitly converted?
Thanks,
Joshua.