[Effective C + +] construction/destructor/assignment operation

Source: Internet
Author: User

Article 05: Understanding C + + silently wrote and called those functions

Please remember:

    • The compiler can secretly create a default constructor for class, a copy constructor, a copy assignment operator, and a destructor
Class Empty{public:    Empty () {...}                              Default constructor    Empty (const empty& RHS) {...}              Copy constructor    ~empty () {...}                             destructor    empty& operator= (const empty& RHS) {...}   Copy assignment operator};

Article 06: If you do not want to use the compiler automatically generated functions should be explicitly rejected

Please remember:

    • In order to dismiss a function that the compilation automatically (secretly) provides, the corresponding member function can be declared private and not implemented. Using a base class like uncopyable is also a practice.

Once you have defined your own constructor, the compiler will no longer automatically generate constructors for you, regardless of function arguments.
If you want to block copying, declare a private copy constructor and copy assignment operator, and do not define them. This way, once someone tries to construct a copy or assign a value, the compiler will give an error.

Article 07: The base class for polymorphism, declaring the virtual destructor

Please remember:

    • Polymorphic (with polymorphic properties) base class should declare a virtual destructor if class has any virtual functions, he should have a virtual destructor.
    • The purpose of Classes design is not to be declared as a virual destructor if it is not applicable as a base class, or not for polymorphic properties.

Any class with the virtual function is almost certain that there should be a virtual destructor as well.
If class does not contain a virtual function, it usually means that it is not intended to be used as a base class. When class does not attempt to be treated as a base class, making its destructor virtual is often a bad idea.

To implement the virtual function, an object must carry certain information, which is used primarily to determine which virtual function is called at run time. This information is usually pointed out by a so-called vptr (virtual table pointer) pointer. Vptr points to an array of function pointers, called VTBL (virtual table): Each class with the virtual function has a corresponding VTBL. When an object calls a virtual function, the actual function that is called depends on the vptr of the object that the vtbl--compiler is referring to to find the appropriate function pointer.

If class contains the Irtual function, the volume of its objects increases (due to PTR). In addition, C + + objects do not have the same structure as the same declarations in other languages (such as C) (because the counterpart of other languages does not have PTR), and therefore cannot pass it to (or receive from) functions written in other languages, i.e. no longer have portability.

Article 08: Don't let exceptions escape destructors

Please remember:

    • Destructors never spit out an exception if a function called by a destructor might throw an exception, the destructor should catch any exceptions, then spit him out or end the program.
    • If it is possible to react to an exception being thrown during the operation of an action function, then class should provide a normal function (rather than a destructor) to perform the operation.

In the case of C + +, where two exceptions exist at the same time, if the program is not finished, it leads to ambiguous behavior. So if an object's destructor might throw an exception, when we try to release a container containing the object of that class, we assume that during the first element of the destructor, an exception is thrown, the other elements in the container are still being destroyed, so they are called each destructor. If another element throws an exception during the destruction process, it is unclear how the story will evolve.

Article 09: Never call the virtual function in the construction and destruction process

Please remember:

    • Virtual functions cannot be called during constructors and destructors because such calls never fall to derived class.

Because the base class constructor executes earlier than the derived class constructor, the member variables of derived class are not initialized when the base class constructor executes. If this period calls the Irtual function down to the derived class, you know that the function of derived class will almost certainly take the local member variable, and those member variables have not been initialized.

Clause 10: Make operator= return a reference to *this

Please remember:

    • The assignment operator returns a reference to *this.

Article 11: Handling "self-assignment" in operator=

Please remember:

    • operator= good behavior when objects are self-assigned, including comparing the addresses of "source objects" and "Destination objects", the thoughtful sequence of statements, and Copy-and-swap.
    • Determines whether any object operates on more than one object, and when multiple objects are the same object, its behavior is still correct.

Clause 12: Do not forget to assign each element of the object when assigning a value

Please remember:

    • The copying function should ensure that all member variables of the object are assigned a base class component.
    • Instead of trying to implement another copying function with one of the copying functions, you should put a common function into the third function and call it together by two functions.

When you write a copying function, make sure (1) Copy all the local member variables, (2) call the appropriate copying constructors within the base class, (3) do not attempt to implement another copying function with one of the copying functions.

[Effective C + +] construction/destructor/assignment operation

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.