Internal call virtual functions for constructors and destructors __ reading notes

Source: Internet
Author: User


calling virtual functions inside a constructor

Calling a virtual function in a normal member function still resolves a virtual function call at run time because the object does not know that it is a class or a derived class that belongs to the member function. In constructors, however, virtual mechanisms do not work, and only "local" virtual functions are invoked for two reasons:

First, in the constructor you can only know that the base class object has already been initialized, but you do not know who will inherit you, and if you call the corresponding virtual member function in the derived class, it may manipulate the member that has not been initialized;

Second, the constructor initializes the vptr first, which can only be initialized based on the base class and the current class, and it will not change in the object's life cycle unless there are other classes to base the class on. If the next constructor is called immediately thereafter, it points the vptr to its own vtable, so backwards, until the last level. When a virtual function call is made, only the current vptr can be used to obtain the corresponding virtual function.


Calling virtual functions in a destructor

As with constructors, virtual functions are invoked in destructors, only local versions are invoked, the basic principle is the same as the constructor, the only difference is that for constructors, the type information is not yet complete, the type information is known when the destructor is called, but the corresponding virtual function is no longer available and has been destructor .

: C15:VirtualsInDestructors.cpp

Virtual calls inside destructors

#include <iostream>

using namespace Std;

Class Base {

Public

Virtual ~base () {

cout << "Base1 ()/n";

f ();

}

virtual void F () {cout << "base::f ()/n";}

};

Class Derived:public Base {

Public

~derived () {cout << "~derived ()/n";}

void F () {cout << "derived::f ()/n";}

};

int main () {

base* bp = new Derived; Upcast

Delete bp;

} ///:~


Run Result:

~derived ()/n

BASE1 ()/n

Base::f ()/n





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.