C + + class construction destructor call sequence Training (review dedicated)

Source: Internet
Author: User

The object does the function parameter//1 studies the copy constructs the//2 to study the constructor function, the destructor call Order//summary construction and the destructor call Order # include "iostream" using namespace Std;class abcd{// This () public:abcd (int A, int b, int c) {this->a = A;this->b = B;this->c = c;printf ("ABCD () construct, A:%d,b:%d,c: %d \ n ", This->a, This->b, this->c);} ~ABCD () {printf ("~abcd () construct,a:%d,b:%d,c:%d \ n", This->a, This->b, this->c);} int Geta () {return this->a;} Protected:private:int a;int b;int c;}; Class Mye{public:mye (): ABCD1 (1, 2, 3), ABCD2 (4, 5, 6), M (+) {cout << "MyD ()" << Endl;} ~mye () {cout << "~myd ()" << Endl;} Mye (const Mye & obj): ABCD1 (7, 8, 9), ABCD2 (Ten, One,), M (+) {printf ("MyD (const MyD & obj) \ n");} PROTECTED://PRIVATE:PUBLIC:ABCD ABCD1; The C + + compiler does not know how to construct abc1abcd abcd2;const int m;}; int dothing (Mye mye1) {printf ("dothing () mye1.abc1.a:%d \ n", Mye1.abcd1.getA ()); return 0;} int run2 () {Mye mye;dothing (mye); return 0;} int Run3 () {printf ("Run3 Start: \ n "); ABCD ABCD = ABCD (100, 200, 300); Initializes an object with a temporary object, it changes the temporary object to the beginning of theInitialized object without invoking the copy constructor, the temporary object does not have to assign itself to the other, and then destroy itself so troublesome.//ABCD (400, 500, 600); The life cycle of the temporary object is printf ("Run3 end\n"); return 0;} int main () {run2 ();//run3 (); System ("pause"); return 0;}

  

Output

C + + class construction destructor call sequence Training (review dedicated)

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.