C + + Learning Note 4, invoking the sequential construction and destructor of a derived class (one)

Source: Internet
Author: User

Test the source code:

Test the call order of the constructors of derived classes when calling//fedora20 gcc version=4.8.2#include <iostream>using namespace Std;class base{public:base () {cout<< "Base created!" <<endl;} ~base () {cout<< "Base destroyed!" <<endl;}};/ /public Inherit class A:public base{public:a () {cout<< "A created!" <<endl;} ~a () {cout<< "A destroyed!" <<endl;}}; Class B:public a{public:b () {cout<< "B created!" <<endl;} ~b () {cout<< "B destroyed!" <<endl;}};

Test code:

int main () {A a;<span style= "color: #ff0000;" >a *a1;</span>cout<< "can see A1 does not call the base class constructor" <<endl;<span style= "color: #ff0000;" >a *a2=new a;</span>//calls the constructor of the base class only when the new object cout<< "can see A3 also does not call the constructor of the base class" <<endl; A *a3=&a;
       b b;}
The output is:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvcxe4ndqzntixntu=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">


You can see that when you create an object of a derived class, you call the constructor in the base class first, and then the constructor that calls the derived class itself.

In the case of a destructor, the order is reversed, the destructor of the derived class is called first, and then the constructor of the base class is called. This is the reason that objects are stored on the stack when objects are created. (The new object, although it exists in the heap, still holds the address in its heap on the stack, so.) The same as the time of destruction)


So, when you create an array of its objects: A a[2], does it call its constructor? That's for sure. For example, the following

Test code: (change only the contents of main)

int main () {A a[2];}
The result is:



What if you create an array that points to its objects? A *a[2];

int main () {A *a[2];}
The answer is obviously not.

So at this point, suppose a has a function fun (), so what happens to A[0]->fun ()?

void Fun () {cout<< "A Fun Run" <<ENDL;}
Test code:

int main () {A *a[2];a[0]->fun ();}
The result is:

Can see that, although the constructor is not called, it is still possible to run its member functions!


So, suppose a has a public int i; A variable, a[0]->i, what would it be?

Class A:public Base{public:int i; A () {cout<< "a created!" <<endl;} ~a () {cout<< "A destroyed!" <<endl;} void Fun () {cout<< ' A Fun Run ' <<endl;}};

Test code:

int main () {A *a[2];a[0]->fun (); Cout<<endl<<a[0]->i<<endl;}

Results:



Can see that a[0]->i points to an indeterminate value. Assume that you specify the static const int I=1; Then the point must be 1.

Let's test the simplest of these today and be a little tired. Multiple inheritance. The imaginary grass will test again tomorrow.

It suddenly seems like the last time the UC written the question. The order of the destruction seems to be mistaken, depressed.

Haven't received the interview information yet. Also has not been the written test hung the notice, also do not know what is a situation ah.

Keep.

Sometimes, the details are important!




C + + Learning Note 4, invoking the sequential construction and destructor of a derived class (one)

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.