Understand the sequence of object constructor calls that are inherited and combined in C + + __jquery

Source: Internet
Author: User
Tags stack pop

Preface:
Hello, the World Good morning, haha, September hello, the first day of September 2015, is also the last year of their undergraduate career, the next is the school recruit to embark on their own work, yesterday is the first class of senior, on the software engineering, very like a class, To have a good understanding of the thought, haha, pull far, down days, but also to work hard, today with an interview titled speaking, Understanding or feeling class on the constructor call order problem:
Let's show the code:

 #include <iostream> using namespace std;
    Class A {public:a () {cout << "a";}

~a () {cout << "~a";}};
    Class B {public:b () {cout << "B";}

~b () {cout << "~b";}
Private:a A;

};
    Class C:public A {public:c () {cout << "C";}

~c () {cout << "~c";}
    Private:b B;
A A;

};
    int main () {cout << "=======================================\n";
    cout << "The construction process of Class A objects:";
    A A;

    cout << "\n======================================\n\n";
    cout << "The construction process of Class B objects:";
    b b;

    cout << "\n=======================================\n\n";
    cout << "The construction process of Class C objects:";
  c *p = new C (); 

    Dynamically requested memory do not forget to release, otherwise the destructor will not be invoked, and any time delete null pointer will not be wrong delete p; Note that if there is no flush buffer here, the last three destructors are executed, but the data is left in the buffer, there is no output//Because there is no flush buffer like cout<<endl; in the destructor, the output without flushing the buffer is as shown in Figure II cout <

    < "\n=======================================\n";
return 0; }

The above program runs the result diagram one:

Figure II: The final destructor has no output to output when the output buffer is not refreshed, the compilation environment g++

Summary:
You can see:
1 The constructor order of the classes with inheritance and combination is: the constructor of the parent class-> the constructor of the composite member object-> its own constructor;
2 The spatial allocation and initialization of the original variables are performed before the call to their own constructors, their declaration initialization order is determined by the declaration order, which is frequently taken in the interview test center in the initialization list of the constructor, noting that it is not determined by the order in which it appears in the initialization list. Rather, the order in which it is initialized is determined by the order in which it is declared in the class;
3 when the object is in the destructor, the invocation order of the destructor is in contrast to the execution order of the constructor, which is easy to understand because the general creation object, such as ClassA A, is created in the stack, the program ends or leaves the scope of the object, and naturally the original stack is emptied. Stack pop-up is from the top of the stack from the stack, note, however, that the object created by new is created in the heap, so the destructor is called when the new object is delete, or the destructor is not invoked, even if it leaves the scope or the end of the program does not call its destructor ;

Note: The above is your own opinion, if there are problems or errors welcome to correct and discuss, thank you

Resources:
http://blog.csdn.net/lazy_tiger/article/details/2093020, thank you.

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.