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 33,186 of 33,346   
   Paul D. DeRocco to All   
   Re: RAII advice (for a RAII newbie)   
   01 Sep 13 00:21:33   
   
   From: pderocco@ix.netcom.com   
      
   > On 8/30/2013 12:28 PM, Richard Phillips wrote:   
   >   
   > Going forward, I'm keen to start using RAII on projects, but in order   
   > to justify my future evangelism, I want to clarify a few things...   
   >   
   > So, borrowing an example from Jon Kalb's excellent tutorial videos:   
   >   
   > // BAD   
   > {   
   > 	dosomething();   
   > 	cleanup();   
   > }   
   >   
   >   
   > // GOOD (RAII)   
   > {   
   > 	CleanupType cleanup;   
   > 	dosomething();   
   > }   
   >   
   >   
   > // ????   
   > {   
   > 	try   
   > 	{   
   > 		dosomething();   
   > 		cleanup();   
   > 	}   
   > 	catch (const std::exception& ex)   
   > 	{   
   > 		cleanup();   
   > 	}   
   > 	catch (...)   
   > 	{   
   > 		cleanup();   
   > 	}   
   > }   
   >   
   > Why is the 2nd (RAII) approach superior to the third approach?  I   
   > presume there's more to it than "appearance"?  While the third   
   > approach looks more complicated, it doesn't have to worry about a   
   > "CleanupType"...   
      
   In addition to what everyone else has said, the 3rd approach is quite a   
   bit less efficient than the 2nd, when an exception is thrown. In some   
   compilers, it is less efficient even when no exception is thrown.   
      
   --   
      
   Ciao,           Paul D. DeRocco   
   Paul            mailto:pderocco@ix.netcom.com   
      
      
         [ 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