"Effective C + +" study notes-clause 17

Source: Internet
Author: User

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




Third, Resource Management

Rule 17:store newed objects in smart pointers in standalone statements

rules 17:< Span style= "Color:rgb (85,85,85); font-family: ' Comic Sans MS '; font-size:18px; Line-height:26px "To place the Newed object in a standalone statement into a smart pointer




1. Phenomena

We want the program to have precedence, so use a function to assign precedence to the object that is being allocated dynamically.

<span style= "Font-family:comic Sans MS;" >int priority ();</span>

<span style= "Font-family:comic Sans MS;" >void Processwidget (std::tr1::shared_ptr<widget> pw, int priority);</span>

You know, we can't use this:

Processwidget (New Widget, priority ());

Instead, it should be:

Processwidget (std::tr1::shared_ptr<widget> (new Widget), priority ());



2. What will the compiler do?

Each argument must be accounted for before the compiler outputs the processwidget calling code. Processwidget The second argument is simple, just call the function, and the first argument is not the same, it is two actions:

--Call new Widget expression

Call Tr1::shared_ptr Constructor


Therefore, the compiler must do the following three things before calling the Processwidget function:

> Call Priority

> Execute "New Widget"

> Calling the Tr1::shared_ptr constructor


The question is, in what order does the C + + compiler perform these three things, it is difficult to judge. It does not perform actions in a specific order like C # or JAVA . In C + +, you can only be sure that the new Widget action takes precedence over the tr1::shared_ptr action.



3. Do you have any questions?

Of course there is a problem, if the order of the three actions is this:

1. Execute "New Widget"

2. Call Priority

3. Call the Tr1::shared_ptr constructor


What happens if an error occurs in the second step that causes an exception? The pointer returned in the first step will be lost, causing a resource leak . Oh no! This big chapter, we have been managing resources, fighting these problems, here again there is a resource leak!



4. So, to solve this problem

solution is also exceptionally simple, do not bother, write two lines. (that is, to separate a sentence into two sentences to write)

< Span style= "line-height:26px" > because, c++ The compiler cannot rearrange the statements, only rearrange the within the statement, which is the question to write:

<span style= "Font-family:comic Sans MS;" >std::tr1::shared_ptr<Widget> PW (New Widget) Processwidget (pw,priority ());</span>



5. Finally, please remember

★ Store newed objects in (placed) smart pointers in separate statements. If you do not, once an exception is thrown, it is possible to cause an imperceptible resource leak.




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

"Effective C + +" study notes-clause 17

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.