Objective C ++ part3.resource Management

Source: Internet
Author: User
13. Use objects to manage resources. Conclusion: first, to prevent resource leakage, use raiI objects to obtain resources in the constructor and release resources in the xigou function. Second, the two commonly used raiI classes are tr1: shared_ptr and auto_ptr. The former is usually a better choice, because its copy behavior is more intuitive. If auto_ptr is selected, the copy action points it to null. 14. think carefully about copying behavior in Resource-managing classes. conclusion: first, the copied raiI object must be copied together with the resources managed by it. Therefore, the copying behavior of the resource determines the copying behavior of the raiI object. Second, the common and common raiI class copying behaviors are: copying suppression and reference counting ). However, other actions may also be implemented. 15. provide access to raw resources in Resource-managing classes. conclusion: first, Apis often requires access to raw resources. Therefore, each raiI class should provide a way to "obtain the resources it manages. Second, access to the original resource may be converted through real or implicit conversion. In general, Explicit conversions are safer, but implicit conversions are more convenient for customers. 16. Use the same form in corresponding uses of new and delete. Conclusion: If you use [] in the new expression, you must also use [] in the corresponding Delete expression. If you do not use [] in the new expression, do not use [] in the corresponding Delete expression. 17. Store newed objects in smart pointers in standalone statements. Conclusion: The newed object is stored in the (placed) function pointer using an independent statement. If not, once an exception is thrown, it may cause imperceptible resource leaks. Perception: This part of content, in addition to the use of more before new and delete, is that the reference count is used more. However, it is not the way in which this book is introduced. It is not so thorough in understanding, because it has never been used before and has never been used before. You need to re-read this part.

Related Article

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.