Effective C + + construction destructor assignment operation

Source: Internet
Author: User

//article 07: Declaring a virtual destructor for a polymorphic base class//1. The destructor of the destructors class is not defined as a virtual function, because a pointer or reference to a base class can point to an object of the derived class, and when the base class object is deleted, an error can occur, resulting in a corrupted data structure. //2. If a class does not contain a virtual destructor, it usually means that it does not want itself to be a base class. //3. Do not declare a virtual destructor for a normal class. This is because C + +, in order to implement virtual functions, creates an array of function pointers, called virtual function tables, which causes the object of the class to carry extra information to determine which virtual function is called during the run, and this extra information is usually made up of a pointer to the virtual function table. //4. By 3, when declaring a virtual destructor for a class, it makes the object of the class occupy more memory, and under 32-bit programs, it will have 4 bytes, and 64 bytes more under the 8-bit program.classctest{ Public:    Virtual~CTest () {} Public:    intValue0; intvalue1;};intSize =sizeofCTest;//size = 16 64-bit program, if you remove the virtual destructor, size = 8;//5. When a class does not contain a virtual destructor (such as a string), do not derive on the basis of this class, or it may cause an exception when the object is deleted. See 1 for reasons. //article 09: Never call virtual functions during construction and destruction//1. The following code, when constructing a member of the Cchild class, the member of its parent class is first constructed, the constructor of its parent class is called first, and a virtual function is called in its parent constructor, and no dynamic binding occurs, and the virtual function called is the version of the parent class. The same is true for object destruction. classcfather{ Public: Cfather () {fun ();} Virtual voidFun () {printf ("Father Virtual");}};classCchild: Publiccfather{ Public: Cchild () {fun ();} Virtual voidFun () {printf ("Child Virtual");}};    Cchild child; //Output Father virtual child virtual and not child virtual child virtual//Article 12: Do not forget every ingredient when copying an object//1. Copy all member variables of this class and call the appropriate copy function of its base class//2. The copy constructor is usually close to the work done by the copy assignment operator, but it is best not to let one call another, and a new function should be defined for the two functions to be called. //3. It is unreasonable to make the copy assignment operator call the copy constructor, because it is like trying to construct an already existing object, which is absurd. //4. It is also unreasonable to make a copy constructor call copy assignment operator, because the copy constructor is used to initialize the new object, and the copy assignment operator is only implemented on the initialized object, so it is logically unreasonable. 

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.