7f57d9b5   
   From: ulrich.eckhardt@dominolaser.com   
      
   Iker Jamardo Zugaza wrote:   
   > I have read that throwing smart pointers to objects is not very clever.   
      
   In what context? URL?   
      
      
   > typedef tr1::shared_ptr SPFoo;   
   >   
   > class FooInherited: public Foo { };   
   >   
   > typedef tr1::shared_ptr SPFooInherited;   
      
   > try   
   > {   
   > throw SPFooInherited(new FooInherited());   
   > }   
   > catch(const SPFoo& f)   
   > {   
   > cout << "SPFoo& caught!" << endl;   
   > }   
      
   Try this code:   
      
    SPFooInherited pfi;   
    SPFoo& pf = pfi;   
      
   This of course doesn't compile, because the two are not related. This is   
   also the reason the catch clause is not considered. When matching catch   
   clauses, the compiler only matches the type, it doesn't perform conversions   
   that would otherwise be considered.   
      
      
   > Everythign compiles but in runtime the second try-catch won't be   
   > executed. Can someone explain me why? Specially if lines of code like   
   > this work perfectly fine in runtime.   
   >   
   > void function(const SPFoo& f)   
   > {   
   > }   
   >   
   > ...   
   >   
   > SPFooInherited fi(new FooInherited());   
   > function(fi);   
      
   See above, when passing an SPFooInherited to a function, a new, temporary   
   SPFoo instance is created. Check the addresses of the original and the   
   function parameter. This is not considered for catching exceptions.   
      
   Notes:   
   - Drop the "smart" from your pointers above and you get the same result.   
   - No need to create a Foo or FooInherited object, i.e. you can also throw   
   [smart] null pointers of the according type.   
   - tr1::shared_ptr is actually smart enough to do the right thing even   
   without a virtual destructor, provided you supply the right parameter to the   
   constructor. You could even store a pointer to an object in a   
   shared_ptr!   
      
      
   Cheers!   
      
   Uli   
      
   --   
   Domino Laser GmbH   
   Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932   
      
      
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|