• Advertisement
Sign in to follow this  

Smart Pointers and Polymorphic Collections

This topic is 3443 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok so this is a problem i stumbled upon the other day. I have a couple of ideas that i will keep to myself for the early part of this topic, so i don't taint the replies. I have a codebase in which everything reference counted, so:
class CB : public class CA
{
};
Then we have declarations like the following:
RefCtd< CA > A;
RefCtd< CB > B;
What is required of class RefCtd for me to be able to store a collection of A's:
std::vector< A > myCollection;
and be able to store smart-pointed entities of derrived classes to it, like so:
myCollection.push_back( InstanceOfB );
Is this accomplishable? If so, what would you do/ have you done? Thanks,

Share this post


Link to post
Share on other sites
Advertisement
if CB inherits from CA, wouldn't it be possible for you to store both CA* and CB* in a smart pointer of type RefCtd< CA >, which in turn you can store in std::vector< RefCtd< CA > > ?
Might be wrong though, or I just missed the point entirely :)

Share this post


Link to post
Share on other sites
It is possible. I would do it by using a pre-existing and tested, reference-counting smart pointer implementation that is known to support this use case: boost::shared_ptr. I would then just create a std::vector<boost::shared_ptr<Base> >, and happily .push_back(boost::shared_ptr<Base>(new Derived())) as desired.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement