Highlights of the effective C + + (iii)

Source: Internet
Author: User

"Effective C + +" Chapter III: Resource Management
  1. Manage resources with objects. A resource, which may be a piece of memory, may be a lock, when the customer needs to manually release after the request is reasonable, it is better to obtain this resource, immediately put it into an object (Raii technology), and then release it in the object's destructor. This frees the operation from being forgotten, and even if there are statements thrown in the context of using the resource class, it ensures that the resources are properly released.
  2. Carefully coping behavior in the resource management class. Some resources do not support copy semantics, while some resources require deep copy semantics, some resources support control transfer semantics, and some resources support reference counting, so copying operation should be extra careful. You should be aware of the copy semantics of Smart pointers (shared_ptr and auto_ptr) in c++98, especially auto_ptr. In C++11, Auto_ptr has been abolished, but two more compatible smart pointers--weak_ptr and unique_ptr have been introduced.
  3. Provides access to the original resource in the resource management class. Although this does not conform to the idea of encapsulation, but in order to be compatible with the old excuses, sometimes have to access the original resources. You can use implicit conversion to access the original resources, you can also use explicit access to the original resources, the two methods have advantages and disadvantages, the implicit conversion to make the user vision more comfortable, but explicit access to more expressive user's intentions, if possible, as far as possible to use explicit access.
  4. The same form is used when using new and delete in pairs. If type *p = new type, then use delete p, or if type *p = new Type[count], then use delete []p. The reason is that the delete operator handles only the first legitimate object at the position where p points, and delete [] ensures that all legitimate objects stored in the position where P points are processed. This is not to say that you can use the delete [] process to get a pointer to a single object from the new type, because some compiler put new type[] to the number of previous bytes before it placed the pointer after the count of legitimate objects!!! So using delete [] to handle the new type gets the pointer more dangerous!!! Please remain symmetrical.
  5. Place the Newed object into a smart pointer with a stand-alone statement. When using a smart pointer as a function argument, do not use this form SomeFunc (shared_ptr (new Type), Otherfunc ()), although C + + guarantees that its parameters will be evaluated before somefunc execution, but does not enforce the valuation order, so If the new type is executed first and then the Otherfunc is executed, and the latter throws an exception, then the memory requested by new type will not be freed because shared_ptr has not yet been able to invoke the constructor, and naturally does not execute its destructor, so the memory leaks, so You need to shared_ptr PW (new Type) before calling SomeFunc (Pw,otherfunc ()). This is the idea of placing the Newed object in a smart pointer with a separate statement.

Highlights of the effective C + + (iii)

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.