"Effective C + +" study notes-clause 15

Source: Internet
Author: User

*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************




Third, Resource Management

rule 15:provide access to raw resources in resource-managing classes

Rule 15: Provide access to the original resource in the resource management class




1. Introduction

The Resource management class (Resource-managing classes) is a good thing, it's like a bulwark against a resource leak. It is the fundamental nature of a good design system to discharge something like a resource leak.

but, Sometimes you have to bypass the resource management object and access the original resources directly.

For example, IN clause 13, we import the idea of using smart pointers (such as auto_ptr or tr1::shared_ptr) to save factory functions, such as Createinvestment's invocation results.

Std::tr1::shared_ptr<investment> PINV (Createinvestment ());
Suppose we use a function to process a investment object:

int daysheld (const investment* PI);    Return to investment failure
But if so called:

int days = Daysheld (PINV);    Error
This is wrong, can't pass the compile, why? What--daysheld needs is a investment* pointer, and we give an object (the object of type tr1::shared_ptr<investment>).

So we need to tune the original resources.


2. Two methods

<1> an explicit conversion

Fortunately, both Tr1::shared_ptr and Auto_ptr provide a GET member function that can be used to get the internal raw pointer.

<span style= "White-space:pre" ></span>int days = Daysheld (Pinv.get ());    Call the original pointer to the function
<2>Implicit conversions

(almost) all smart pointers overload the pointer-to-value operators (operator-> and operator*), which allow the implicit conversion to the original pointer at the bottom of the pointer:


<span style= "FONT-SIZE:14PX;" >c</span><span style= "Font-family:comic Sans ms;font-size:14px;"    >lass Investment {Public:bool istaxfree () const;    ...};investment* createinvestment ();std::tr1::shared_ptr< investment > Pi1 (createinvestment ()); Make TR1::SHARED_PTR manage a resource bool Taxable1 =!    (Pi1->istaxfree ()); Access resources through operator-> ...std::auto_ptr<investment> Pi2 (Createinvestment ()); bool Taxable2 =!    ((*PI2). Istaxfree ()); Access resource </span> via operator*; 




3. Implicit conversion functions

< Span style= "FONT-SIZE:14PX; font-family: ' Microsoft Yahei '; White-space:pre "> Some designers provide an implicit conversion function because there are times when primitive resources within RAII objects must be obtained. The following is a RAII class for fonts

<span style= "Font-family:comic Sans ms;font-size:14px;" >fonthandle GetFont ();    C API Provisional parameter void Releasefont (Fonthandle fh);    From the same group C Apiclass font  {public:    explicit font (Fonthandle FH): F (FH)    //using Pass-by-value to get resources    {  }< C8/>~font ()  {  releasefont (f);  }    Free Resources private:    fonthandle F;    Raw Font Resource}</span>

Assume that converting a "Font object to Fonthandle" becomes a frequent requirement. The Font class can provide an explicit conversion function for this purpose:

<span style= "Font-family:comic Sans ms;font-size:14px;color: #666666;" >class Font  {public:    ...    Fonthandle get () const  {  return f;  }    ...}; </span>

However, if the client wants to use the API every time, it calls get:

<span style= "Font-family:comic Sans ms;font-size:14px;color: #666666;" >void Changefontsize (fonthandle F, int newSize); Font f (GetFont ()); int newfontsize;...changefontsize (F.get (), newfontsize);    Convert a font to fonthandle</span>

Here's another way to make the font provide an implicit conversion function that transforms to Fonthandle:

<span style= "Font-family:comic Sans ms;font-size:14px;color: #666666;" >class Font  {public:    ...    operator Fonthandle () const    {  return f;  }    ...}; Font f (GetFont ()); int newfontsize;...changefontsize (f, newfontsize);</span>

However, there are some errors that can occur, such as when a user accidentally creates a fonthandle when a font is required,

And if there is a fonthandle like below that is managed by the Font object F1, this object can also be obtained by using F2 directly. If the F1 object is destroyed F2 will become a dangle, empty shell.

Font F1 (GetFont ()); Fonthandle F2 = F1;



4. Summary

whether to provide an explicit conversion function (such as a Get member function) to convert the Raii class to a bottom resource, or to provide an implicit conversion function, depends primarily on the specific work that the Raii class is designed to perform, and the situation in which it is used. The best design should be to adhere to clause 18 advice: make the interface easy to use, not easy to misuse.

Sometimes, it may be felt that the return primitive resource functions within the RAII class contradict the encapsulation. This is correct, but it is not a design error, to be clear that the RAII class does not exist to encapsulate something, their task is to ensure the release of resources , and perhaps sometimes with some resource encapsulation, but that is not necessary, not its fundamental task.

As with other good classes, It hides the parts that the customer doesn't need to see, but has everything the customer needs . .


5. Please remember

☆apis often require access to the original resources, so each RAII class should provide a way to "get the resources it manages".

☆ Access to the original resource may be explicitly converted or implicitly converted. Explicit conversions are generally more secure, but implicit conversions are more convenient for customers.




*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree********************************************

"Effective C + +" study notes-clause 15

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.