Efficient C + +--experience clause (iii)

Source: Internet
Author: User

Efficient C + +--experience clause (iii)

Declare the virtual destructor for the polymorphic base class:

"Give base classes a virtual destructor", this rule applies only to base classes with polymorphic properties. This base classes is designed to be used to "process derived class objects through the base class interface."

Not all base classes are designed for polymorphic purposes. For example, Standard STIRNG and STL containers are not designed to be used as base classes, let alone polymorphic. Some classes are designed to be used as base classes, but not for polymorphic purposes. Some classes are not designed to "dispose of derived class objects through the base class interface", so they do not need a virtual destructor.

BaseClasses with polymorphic properties should declare a vitual destructor. If class has any virtual functions, he should have a virtual destructor.

The purpose of classes is not to declare a virtual destructor if it is not used as a baseclasses, or if it is not for polymorphism.

Do not invoke the virtual function in a constructor or destructor

Return the operator= operator to reference *this

Note that "self-assignment" is handled in operator=

In a custom operator=, it is important to make sure that all members are replicated, especially if there is a secondary relationship.

The standard library auto_ptr similar to the smart pointer unique_ptr, he can only point to the only object, if copied by the copy constructor or copy assignment operator, they will become null, and the copied pointer will take the resources of the unique power. Resources managed by auto_ptr must have absolutely no more than one auto_ptr pointing to it at the same time.

Auto_ptr and shared_ptr both do delete instead of delete[in their destructor, which means that using auto_ptr or shared_ptr on a dynamically allocated array is not a good idea.

In fact, new and delete have a lot of methods and meanings in use, here is the most classic use of a method, using new dynamic generation of an object:

When you dynamically generate an object using new, two things happen. First, memory is allocated (through a function called operator new), and second, there is a (or more) constructor called for this memory. When you use Delete, two things happen, one (or more) destructors are called for this memory, and then the memory is freed (through a function called operator delete). The biggest problem with delete is: How many objects are there in the memory that will be deleted? The answer to this question determines how many destructors must be called. That is, the pointer that is about to be deleted, refers to a single object or an array of objects?

There is a problem, if you use [] in the new expression, you must also use [] in the corresponding delte expression. If you do not use [] in the new expression, you must not use [] in the corresponding delete expression.

Remember, this is the first way to use new, and we'll show you how to use the new method in the future.

When a smart pointer is used, the Newed object is stored in a smart pointer in a separate statement. In other words, for example, if one parameter of a function is a smart pointer, do not complete the pointer-pointer amount initialization at the time of passing the parameter, initialize it first, and then pass the initial good thing. Otherwise, it may cause errors.

Efficient C + +--experience clause (iii)

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.