Call of C ++ constructor destructor

Source: Internet
Author: User

Yesterday I saw an interview with the moto company with the following questions:

# Include <iostream>

Using namespace STD;

Class human

{

Public:

Human ()

{

Human_num ++;

}

Static int human_num;

 

 

 

 

~ Human ()

{

Human_num --;

Print ();

}

Void print ()

{

Cout <"Human nun is:"

}

Protected:

PRIVATE:

};

Int human: human_num = 0;

Human F1 (Human X)

{

X. Print ();

Return X;

}

Int main (INT argc, char * argv [])

{

Human H1;

H1.print ();

Human H2 = F1 (H1 );

H2.print ();

Return 0;

}

Write the output result of the program ..... Only five rows are output, but what is the value of human_num in each row ??

Let's not look at this question first. Let's take a look at how the constructor transfers data to the function by value and what will happen when it returns data from the function by value:

1. When the object is passed to the function by value, a copy of the object will be created first, and the copy will be passed to the function. However, the constructor will not be called when the copy is created, the Destructor will be called when this function returns.

2. When a function returns an object by value, it will also create a copy of the returned object. Likewise, it will not call the constructor. After the return, it will call the destructor to destroy the object.

Let's first look at the following program:

/*
Constructor and destructor
*/
# Include <iostream>
# Include <string>
Using namespace STD;

Class Test
{
Public:
// Constructor
Test (string S): STR (s)
{
Cout <"constructing" <Endl;
}

// Destructor
~ Test ()
{
Cout <"destructing" <Endl;
}

String getvalue ()
{
Return STR;
}

PRIVATE:
String STR;
};

Void display (test T)
{
Cout <t. getvalue () <Endl;
}

Test gettest ()
{
Return test ("Jun ");
}

Void main ()
{
// Here it is obvious that a constructor is called, and the program will output: Constructing
Test TE = test ("guo ");

// When you pass an object to a function by value, a new copy of the object is created.
// But the constructor is not called at this time. No constructing output
Display (TE );
// When the function returns, it will call its destructor to destroy the previous copy.
// Program output: destructing

Gettest ();

// In gettest, we create an object, so there will be a constructor call.
// Program output: Constructing. We know that when the program returns, a copy will be generated to return
// In fact, there will be a destructor call in the function, and then return to the main function.
// The object will also be destructed, so it will output destructing again, and finally destroy the original

// Object.

}

// Result of the final program:
// Constructing
// Destructing
// Guo
// Destructing
// Constructing
// Destructing
// Destructing
// Destructing

Let's look at the example above:

# Include <iostream>

Using namespace STD;

Class human

{

Public:

Human ()

{

Human_num ++;

}

Static int human_num;

~ Human ()

{

Human_num --;

Print ();

}

Void print ()

{

Cout <"Human nun is:"

}

Protected:

PRIVATE:

};

Int human: human_num = 0;

Human F1 (Human X)

{

X. Print ();

Return X;

}

Int main (INT argc, char * argv [])

{

Human H1;

// H1 constructed, human_num = 1

H1.print ();

// H1 make a copy and pass the copy to the function. No constructor is called, so 1 is output.

// The returned object is an object, so a copy is still made and then returned. At this time, the stream generation function is called, and the temporary copy is destructed. human_num = 0, so h2.print (), similarly, 0 is output, and H1 and H2 are destructed.-1 and-2 are output respectively.

Human H2 = F1 (H1 );

H2.print ();

Return 0;

}

Related Article

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.