the danger comes from code like so: void foo(boost::shared_ptr<entity> p) { ... } void bar() { entity* e = new entity; foo(e); delete e; } Allowing a pointer to be converted to a shared_ptr implicitly could cause serious bugs. You could shorten things with this (untested) function: template <typename T> boost::shared_ptr<T> make_sp(T* p) { return boost::shared_ptr<T>(p); }
-----Original Message----- From: Maurizio Colucci [mailto:seguso@email.it] Sent: Friday, March 28, 2003 8:36 AM To: Boost-Users@yahoogroups.com Subject: [Boost-Users] Why "explicit" in shared_ptr's constructor?
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)));
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
------------------------ Yahoo! Groups Sponsor ---------------------~--> Get 128 Bit SSL Encryption! http://us.click.yahoo.com/xaxhjB/hdqFAA/xGHJAA/EbFolB/TM -------------------------------------------------------------- -------~->
Info: http://www.boost.org Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl Unsubscribe: mailto:boost-users-unsubscribe@yahoogroups.com
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
----------------------------------------------------------------------- DISCLAIMER: Information contained in this message and/or attachment(s) may contain confidential information of Zetec, Inc. If you have received this transmission in error, please notify the sender by return email. -----------------------------------------------------------------------