Effective C + + Note (ii): construction/destructor/assignment operation

Source: Internet
Author: User

Reference: http://www.cnblogs.com/ronny/p/3740926.html clause 05: Understanding what functions C + + silently writes and calls

If you customize an empty class, the default constructor, copy constructor, copy assignment function, and destructor are automatically generated (again, it is too wordy to feel the original translation!). )。

When a const object or reference type is in a member variable, the compiler cannot synthesize the default copy assignment function, and when a base class defines its copy assignment function as private, its derived class

The default copy assignment function cannot be generated because it cannot complete the assignment of the base class component.

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

If you do not want the compiler to automatically generate default functions, you can declare the corresponding member functions private and not implemented (otherwise member functions or friends can still be called), or they can be implemented in a base class and privately inherited

C++11 has abandoned the above practice, but by deleting the function to set, only need to add =delete after the corresponding function list.

Article 07: Declaring a virtual destructor for a polymorphic base class

when you need to manually delete a pointer to the base class, you need to set the destructor of the base class to virtual, which allows the delete to dynamically remove members of the possible derived class.

Do not declare a destructor for a class without virtual member functions as virtual, because if you want to implement the virtual function, you must let the class carry more information (storage space).

The standard string does not contain any virtual functions, so do not define a string as the base class for a defined class. Similar situations occur in other STL containers such as Vector,list,set.

Declare a pure virtual function for a smoke-like base class (cannot define an object entity), and provide a definition for this pure virtual function. That is, if a class has any one virtual function, it needs to define

A virtual destructor.

Not many of the base class designs are intended for polymorphism (handling derived class objects through the base class interface), so they do not require a virtual destructor. That is, if a class is not a base class or is not a

Polymorphic to use, you should not declare a virtual destructor.

Effective C + + Note (ii): construction/destructor/assignment operation

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.