Boost logo

Boost Users :

From: Maurizio Colucci (seguso_at_[hidden])
Date: 2003-03-28 11:36:07


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<shared_ptr<entity> > 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)));

which is less readable and, IMHO, barely tolerable on the long
run: too much typing.

It seems that the reason is the explicit keyword in the
constructor. My question is: why? what was the danger without the
explicit?

Thank you very much for any info,

Maurizio


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net