The construction of virtual function by class and member function call the execution process of virtual function _c language

Source: Internet
Author: User

Copy Code code as follows:

#include <iostream>

Class base{
Public

Base ()
{
std::cout<<std::endl;
std::cout<< "Base constructor" <<std::endl;
Func1 ();
std::cout<<std::endl;
}

Virtual ~base ()
{
std::cout<<std::endl;
std::cout<< "Base Distructor" <<std::endl;
Func1 ();
std::cout<<std::endl;
}
virtual void func1 ()
{
std::cout<< "Base virtural func1" <<std::endl;
}

void Func2 ()
{
std::cout<< "Base member Func2" <<std::endl;
Func1 ();
std::cout<<std::endl;
}
};

Class Derived:public base{
Public
Derived ()
{
std::cout<<std::endl;
std::cout<< "derived constructor" <<std::endl;
Func1 ();
std::cout<<std::endl;
}

Virtual ~derived ()
{
std::cout<<std::endl;
std::cout<< "Derived Distructor" <<std::endl;
Func1 ();
std::cout<<std::endl;
}

virtual void func1 ()
{
std::cout<< "derived virtual func1" <<std::endl;
}

};

int main ()
{
Base *point = new derived ();
POINT->FUNC2 ();
Delete point;
return 0;
}

There will be such an output


Even if FUNC1 is a virtual function, in the base class and derived constructors and destructors, they call the func1 within their own classes.

And in the ordinary member function Func2 call FUNC1, will walk the virtual function flow.

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.