Re: [Boost-users] Collections of shared_ptr<Base> and shared_ptr<Derived>

Folks, I have come up with the following solution in template form. I would be ecstatic to receive any feedback on either my solution or original question. I am not an expert in these things and I can use all the help any generous soul could provide. Here is my solution: // Function template to create a vector<shared_ptr<Base> >* from // a vector<shared_ptr<Derived> > template<typename Base, typename Derived> vector<shared_ptr<Base> > *derivedSharedPtrVectorToBase(vector<shared_ptr<Derived> > const &dVec) { vector<shared_ptr<Base> > *bVec = new vector<shared_ptr<Base> >(dVec.size()); for(int i = 0; i < dVec.size(); i++) (*bVec)[i] = dVec[i]; return(bVec); } Thanks for your time, KW ________________________________ From: Keith Weintraub Sent: Wednesday, November 16, 2005 10:17 AM To: 'boost-users@lists.boost.org' Subject: Collections of shared_ptr<Base> and shared_ptr<Derived> Folks, What is the best way to pass a std::vector<boost::shared_ptr<Derived> > to a function that expects a std::vector<boost::shared_ptr<Base> >? Can I use a cast or should I create a new std::vector<boost::shared_ptr<Base> > by iterating over the std::vector<boost::shared_ptr<Derived> >? Here is some sample code. What f is doing is not important it is just an example: class A { // This class is from a library I have no control over. public: virtual string getName() { return string("A");} }; class B : public A { // I need to create B from A to have extra functionality public: virtual string getName() { return string("B");} virtual string getLongName() { return string("This is B");} }; void f(std::vector<boost::shared_ptr<A> > &v) { // This function is from a library that I have no control over. for(int i = 0; i < v.size(); i++) cout << v[i]->getName(); } int main(char *argv, int arc) { std::vector<boost::shared_ptr<B> > bvec(10); boost::shared_ptr<B> bPtr(new B); bvec[0] = bPtr; f(bvec); // This should not compile. <------------------------- } The last line (with the arrow) won't compile because std::vector<boost::shared_ptr<B> > cannot be converted to std::vector<boost::shared_ptr<A> >.

On 11/21/05, Keith Weintraub <Keith@archeuscap.com> wrote:
Folks,
I have come up with the following solution in template form. I would be ecstatic to receive any feedback on either my solution or original question. I am not an expert in these things and I can use all the help any generous soul could provide.
Here is my solution:
*snip* It would seem you're overengineering things. It'd be simpler just to use std::vector's iterator constructors: std::vector< boost::shared_ptr< derived > > data; ... std::vector< boost::shared_ptr< base > > downcast_data( data.begin() , data.end() ); If you wanted to go with your original version, I'd suggest returning a managed pointer of some sort (std::auto_ptr or boost::shared_ptr) to decrease the likelyhood of a leak - or having the user pass a nonconstant reference to a vector to fill. Sorry if this has allready been covered by the time this is recieved, my posts seem to still be awaiting moderator approval. -Mike
participants (2)
-
Keith Weintraub
-
Michael Rickert