home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,403 of 33,346   
   Iker Jamardo Zugaza to All   
   Smart pointers and exceptions   
   24 Aug 11 16:44:21   
   
   7f57d9b5   
   From: ijamardo@deusto.es   
      
   Dear all,   
      
   I have looked over the internet and this thread looking for a complete   
   answer of this situation I am facing. I have read that throwing smart   
   pointers to objects is not very clever. I just want to understand why   
   is this happening. I will explain the situation. Let's imagine this   
   simple hierarchy:   
      
   class Foo   
   {   
   public: virtual ~Foo() {}   
   };   
      
   typedef tr1::shared_ptr SPFoo;   
      
   class FooInherited: public Foo { };   
      
   typedef tr1::shared_ptr SPFooInherited;   
      
   And let's check this test code:   
      
   int main(int argc, char** argv)   
   {   
     try   
     {   
       throw FooInherited();   
     }   
     catch(const Foo& f)   
     {   
       cout << "Foo& caught!" << endl;   
     }   
      
     try   
     {   
       throw SPFooInherited(new FooInherited());   
     }   
     catch(const SPFoo& f)   
     {   
       cout << "SPFoo& caught!" << endl;   
     }   
      
     return 0;   
   }   
      
   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);   
      
   Thank you very much in advance.   
      
   Regards,   
   Iker.   
      
      
   --   
         [ 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)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca