C ++ inherits the calling sequence and virtual destructor of the destructor in the destructor.

Source: Internet
Author: User

C ++ inherits the calling sequence and virtual destructor of the destructor in the destructor.

First, let's talk about the constructor. We all know that the constructor can call member variables, and the Child class in inheritance is to turn the members of the base class into their own members, that is to say, the subclass can call the members of the base class in the constructor. This means that the base class constructor must be called before creating the subclass, only in this way can the subclass use the base class members in the constructor. Therefore, when creating a subclass, the base class constructor is called before calling its own constructor. In layman's terms, you must use certain items, but you cannot produce these items by yourself. Naturally, you must wait for others to produce them before using them.

The next step is the destructor. As mentioned above, the subclass turns the base class members into its own members, then the base class will only exist in the subclass until the subclass calls the destructor.Make a hypothesis: If the destructor of the base class is called before the subclass, what will happen? The class member is terminated, but the class itself is still there. But if the class exists, the class member should still exist. Isn't that a conflict?Therefore, the subclass calls its own destructor and then calls the destructor of the base class.The destructor of the base class must be set to virtual, and the final subclass can be virtual or virtual, because no other class inherits from it will not affect the final function.However, not all class destructor are set to virtual, because an extra virtual table pointer will be added during class instantiation of virtual functions, wasting memory performance.

Now the virtual function is used, and the main role of virtual is in polymorphism, and the major cause of C ++ polymorphism is the dynamic binding of classes, dynamic binding refers to converting the pointer or reference of a subclass into a base class, and the base class object can dynamically determine which subclass member function to call. This indicates that virtual has no meaning (except pure virtual functions) if there is no subclass pointer or reference conversion for the base class object, that is, whether or not virtual is to call its own member functions. Through these analyses, you will have an eye on virtual. When a subclass pointer or reference is converted to a base class, if a virtual defined function is used in the base class, after the quilt class is rewritten, the base class object will call the rewritten function in the subclass according to the subclass, instead of functions in the base class; otherwise, if the base class is not defined by virtual, no matter which subclass value the base class is assigned, all base-class member functions are called (of course, the base-class function that is overloaded by the value subclass, otherwise the member functions that are unique to the subclass will not be compiled ).

Why is there no virtual constructor in a virtual destructor?

The constructor cannot be a virtual function, because the constructor itself is also called when the constructor class is constructed. Then, the constructor class will call the base class constructor, therefore, the existence of virtual constructor is meaningless. Only after the construction is complete can the object become a real instance of the class name. In addition, static member functions and inline functions cannot be virtual functions.

Virtual functions are for objects rather than classes.

This can be seen from the fact that a class member function (that is, a static member function) cannot be a virtual function. If a class is not instantiated as an object, the existence of a virtual function is meaningless.


The above assumption is not recognized by me. The constructor In the derived class can call the constructor of the base class, which is implemented in Compiler compilation. that is, the default base class constructor is automatically added at the beginning of the subclass constructor or the base class constructor call specified in the initialization list; the base class destructor is automatically added at the end of the subclass destructor.

As to why the base class constructor is called before the subclass constructor, the base class constructor is called first. in my opinion, only the existence of base class members in the Child class may occur, and the existence of base class members dependent on the Child class members will not occur. for example, a member in the subclass is a reference of the object pointed to by a pointer member in the base class. in this case, if the base class constructor is not called to initialize its pointer member to create an object. then, when the subclass references initialization, it does not know where it will point. similarly, if the base class is called to release the objects in the hierarchy, the reference variable in the subclass does not make any sense when doing the final work, therefore, the pointing object has been released. In a derived class object, the base class member exists before the child class member, and then the Child class object disappears.
Execution Result Analysis of C ++ programs, constructor, destructor, and copy the execution sequence of Constructor

Void main ()
{
Point m (), p (); // the constructor function appears twice.
Point n (m); // copy the constructor function for one calling of the copy_initialization.
P = move (n); // first construct a temporary object, and then copy n to this temporary object to execute the calling the copy_initialization constructor function.
Then execute the move function to output OK! Build a point r to output the calling the constructor function once.
Then, the move function call completes the destruction of the temporary object and outputs a calling the destructor function. the destroy object r outputs a calling the destructor function to destroy the class assigned to the temporary object and outputs a calling the destructor function.
Returns an r value to p.
Output the calculated coordinates, p = 25, 60, and destroy the data in the first three objects.
Don't know, ask again and adopt it in time

Sequence of constructor and destructor calls

Test obj1, obj2; // when you define two objects, the constructor is automatically called, that is, you can see that there are two constructor prints.
Obj1.print (); // not explained
Obj2.print (); // not explained
// When the scope of the defined object ends (lifecycle), the system automatically calls the destructor to release it.
So obj1 and obj2 are released again at the end of main.

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.