More-efficient tive-c ++ sequence exception

Source: Internet
Author: User

Idea: manage resources with local objects. When an exception occurs during a call, use the policy that must be called by the destructor of a local object to release resources in the destructor. A local object stores [resources that must be automatically returned] and returns resources using the object's destructor. Auto_ptr, scoped_ptr, auto_lock, FilePtr, and HandlePtr are all of these ideas. This is the sample code. [Cpp] # include # Include # Include Using namespace std;/* Author: lpstudy Date: 2013-3-16 content: more efficient C ++ exception chapter 9th, using partial object destructor to release resources, ensure that resources are safely released when exceptions occur. */# Define TRACE_FUCTION_AND_LINE (fmt ,...) printf ("[% 20 s: % 4d]" fmt "\ n" ,__ FUNCTION __, _ LINE __, ##__ VA_ARGS __) // Widget is just a simple test class and serves as an instantiation parameter of the auto_ptr template. Auto_ptr is used to manage the new Widget memory and delete class widgets {public: Widget (int nHight = 0): m_nHight (nHight) {TRACE_FUCTION_AND_LINE ();}~ Widget () {TRACE_FUCTION_AND_LINE ();} void Log () {TRACE_FUCTION_AND_LINE ("My Log ------ % d", m_nHight);} int m_nHight ;}; // FilePtr receives a FILE handle and closes the handle when the object is parsed. Class FilePtr {public: explicit FilePtr (FILE * f = NULL): m_hFile (f) {TRACE_FUCTION_AND_LINE ("m_hFile: % 08 p", m_hFile );}~ FilePtr () {if (m_hFile) {TRACE_FUCTION_AND_LINE ("close m_hFile: % 08 p", m_hFile); fclose (m_hFile) ;}} FILE * Get () {return m_hFile ;} private: FILE * m_hFile;}; int main () {TRACE_FUCTION_AND_LINE ("Trace auto_ptr ....... "); auto_ptr AutoWight (new Widget (100 )); AutoWight-> Log (); TRACE_FUCTION_AND_LINE ("Trace FilePtr ....... "); FilePtr filePtr (fopen (" data.txt "," a + "); char buf [100] =" Data Test... \ n "; fwrite (buf, 1, strlen (buf), filePtr. get (); return 0;} I hope I have not obtained this idea, or I have not fully understood this idea. Let's take a closer look at this simple example. If you want to learn more about auto_ptr and lock, we recommend that you read scoped_ptr and autolock.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.