Call order and virtual destructor of destructor constructors in C + + inheritance

Source: Internet
Author: User

first talk about constructors. We all know that the constructor can call the member variables, and inherit the neutron class is to make the members of the base class their own members, so that the subclass in the constructor can call the members of the base class, which means that the creation of subclasses must first call the base class constructor, Only such subclasses can use the members of the base class in the constructor, so the constructor of the base class is called first when the subclass is created and then its own constructor is called. Popular Point says, you need to use certain items. But these things you can not make their own production, nature will wait for others to produce, you have the ability to use.

if: if the destructor of the base class is called first than the subclass. So what's going to happen? The class member terminates, and the class itself is still present, but in the case of the class. The class members should still exist, does that create a contradiction? so a subclass is a destructor that calls its own destructor and then calls the base class. but not all of the destructors for the class are set to virtual better. A virtual table pointer is added as a result of a class instantiation of a virtual function. Wasting memory performance.

Now to the virtual function. The main function of virtual is in polymorphism, and the most basic of C + + is the dynamic binding of class, while dynamic binding refers to converting a pointer or reference of a subclass into a base class, the base class object can dynamically infer which subclass member function is called.

This means that there is no child class pointer or reference converted to a base class object. Virtual does not have any meaning (except pure virtual functions). That is, there is no virtual call to its own member function. Through these analyses, there is a case for virtual.

When a subclass pointer or reference is converted to a base class, a function in the destructors class that is useful for virtual definition is overridden after the quilt class. This base class object invokes the overridden function in the subclass, not the function in the base class, and, conversely, if the base class does not have a practical virtual definition, the call is a member function of the base class, regardless of the value of the subclass that the base class is assigned to (of course, the base class function of the value subclass overload. Otherwise the subclass-specific member function will be compiled only if it is called).

Why does a virtual destructor exist without a fictional function?

A constructor cannot be a virtual function, because it is also a subclass constructor that is called when the subclass is constructed, and then the subclass constructor calls the base class constructor. So there is no point in the existence of fictional creation functions.

Only after the construction is complete, the object's ability becomes a real instance of the name of a class.

In addition, static member functions and inline functions cannot be virtual functions.

Virtual functions are object-specific, not class-specific.

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


Above if I do not feel recognized, constructs in derived classes, destructors can be called to the base class, the construction of the destructor is implemented by compiler compilation. That is:at the beginning of the subclass constructor, you actively join the default base class constructor or the base class constructor call specified in the initialization list, and at the end of the subclass destructor, you actively join its base class destructor call.

As for why the base class constructor is called before the subclass constructor is called, the subclass destructor is called before the base class destructor is called. I think:The existence of a member-dependent subclass member in a base class does not occur because there is only a possibility that a member of a subclass is dependent on the existence of a base class member.For example, a member of a subclass is a reference to an object that a pointer member of a base class points to. This is the case if the base class constructor is not first called to initialize the object with its pointer member initialization. The subclass reference is initialized without knowing where it will be. If the base class is first called to release the object in the same destructor, In this case, the reference variable in the subclass does not have any meaning in doing the aftercare, so its pointing object has been released. The base class member in the derived class object exists before the child class member, and then disappears after the subclass object.

Call order and virtual destructor of destructor constructors in C + + inheritance

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.