c++ - Trouble switching from vector of dumb pointers to boost::shared_ptr -
alright, has had me stumped on , off couple of months now, i'll see if else can this.
so in main program have 2 kinds of structs (solarbody_t, ship_t) both derived same base class (physical_t). make vector of both objects, since can't put solarbody , ship in same vector. both dumb pointer vectors.
tried putting both in same vector, using vector of boost::shared_ptrs. now, understanding, shared_ptrs should have same semantics , syntax dumb pointers (e.g. foobar->mass = 42; should work both). however, changing declaration vector of boost::shared_ptr dumb pointers, gives me error when try , push_back vector of shared_ptrs.
from can tell, should work. boost docs give example of
boost::shared_ptr<int> p(new int(2)); which pretty i'm doing.
has had previous experiences this? or want suggest way store in vector?
for more details, here's gist of it (kind of contradiction of terms, made pun, there.)
i don't think it'll let automatically construct shared_ptr bare pointer. push_back expecting shared_ptr<foobar_t>, not bare foobar_t. should take @ boost::make_shared , try this:
entity_smart.push_back(boost::make_shared<foobar_t>(42)); make_shared has few advantages: namely, allocates pointer control block , object in 1 allocation , keeps unmatched new out of code. makes explicitly clear you're creating shared_ptr object.
other that, yes, semantics should same. keep in mind shared_ptr may overkill you're doing, though, if don't need share ownership of objects.
Comments
Post a Comment