Resource Acquisition is initialization (raiI? (Encapsulation advantages)

Source: Internet
Author: User

Let's take a look at a short section.CodeIt is taken from Bjarne stroustrup's speech "speaking C ++ as a native ":

// Use an object to represent a resource ("resource acquisition is initialization ")

class file_handle {// belongs in some support library
file * P;
public:
file_handle (const char * PP, const char * r)
{P = fopen (PP, R); If (P = 0) throw cannot_open (PP) ;}< br> file_handle (const String & S, const char * r)
{P = fopen (S. c_str (), R); If (P = 0) Throw cannot_open (PP) ;}< br> ~ File_handle () {fclose (p);} // destructor
// copy operations and access functions
};

Void F (string S)
{
File_handle file (s, "R ");
// Use File
}

Those who are familiar with C ++ are certainly familiar with this raiI technique. To put it simply, raiI's general practice is to obtain resources during object construction, and then control the access to resources so that they remain valid throughout the object's lifecycle, finally, resources are released during object analysis. In this way, we actually managed the responsibility for managing a resource to an object. This approach has two benefits:

First, we do not need to release resources explicitly. Taking function F in the above Code as an example, we don't have to worry about "Forgetting" to close the file. In addition, even if the control structure of function f is changed, for example, inserting return in the middle of the function or throwing an exception, we can determine that the file will be closed. RaiI is a powerful weapon to achieve exceptional security, especially when the sky is full. Similarly, if a class C contains a file_handle Member, we do not have to worry that the class C object will "forget" to close the file when it is destroyed.

Second, in this way, the resources required by the object are always valid during its life cycle-we can say that this class maintains an invariant. In this way, you do not have to check the validity of resources when using such objects, which can simplify the logic and improve efficiency.

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.