Maurizio Colucci wrote:
Hello,
I am a newbie user of boost.
I have a question about boost::shared_ptr. Suppose you have a polimorphic list:
class entity{ ...};
class simple_entity : public entity{ public: uint id; simple_entity(uint i) : id(i) {} };
int main(){ list
l; ... I noticed the following code doesn't compile
l.push_back(new simple_entity(1));
too bad, because it was very easy to read, and very similar to the behavior if standard pointers (not to speak about similarity to Java and C#). So I am forced to do
l.push_back(shared_ptr<entity> (new simple_entity(1)));
The idiomatic way to write the above is to use a named variable:
shared_ptr<entity> pe(new simple_entity(1));
l.push_back(pe);
as explained in
http://www.boost.org/libs/smart_ptr/shared_ptr.htm#BestPractices
If you want to save typing, consider adding
static shared_ptr