The object constructor and destructor are called differently in different situations.

Source: Internet
Author: User

The calling of constructor and destructor varies greatly in different situations.

The number of constructor and destructor calls is different.

 

When the object is used as a parameter, the real parameter is completely copied to the form parameter, so no need to call the constructor

In fact, it is called the copy constructor cClass A (); cClass B =;

 

However, when the parameter is running as a temporary variable, you must call the destructor.

However, when an object is referenced as a parameter, because it is the original real parameter itself, the Destructor is not called in the function.

 

# Include <iostream> <br/> using namespace STD; </P> <p> class Aclass <br/>{< br/> Public: <br/> int; <br/> Aclass (int t = 0) {<br/> This-> A = T; <br/> cout <"constructor" <A <Endl; <br/>}< br/> void print () {<br/> cout <"print" <this-> A <Endl; <br/>}< br/> ~ Aclass () {<br/> cout <"destructor" <A <Endl; <br/>}</P> <p> }; </P> <p> Aclass test (Aclass para) // attention please !!! <Br/>{< br/> Aclass TMP (4); <br/> TMP. print (); <br/> TMP. A = para. A + 1; <br/> para. print (); <br/> return TMP; </P> <p >}< br/> void main () <br/>{< br/> Aclass T1 (2); <br/> Aclass T2; <br/> T2 = test (T1 ); <br/> t1.print (); <br/> t2.print (); </P> <p >}< br/>

The result is:

Constructor 2
Constructor 0
Constructor 4
Print 4
Print 2
Destructor 3
Destructor 2
Destructor 3
Print 2
Print 3
Destructor 3
Destructor 2
Press any key to continue

 

# Include <iostream> <br/> using namespace STD; </P> <p> class Aclass <br/>{< br/> Public: <br/> int; <br/> Aclass (int t = 0) {<br/> This-> A = T; <br/> cout <"constructor" <A <Endl; <br/>}< br/> void print () {<br/> cout <"print" <this-> A <Endl; <br/>}< br/> ~ Aclass () {<br/> cout <"destructor" <A <Endl; <br/>}</P> <p> }; </P> <p> Aclass test (Aclass & para) // attention please !!! <Br/>{< br/> Aclass TMP (4); <br/> TMP. print (); <br/> TMP. A = para. A + 1; <br/> para. print (); <br/> return TMP; </P> <p >}< br/> void main () <br/>{< br/> Aclass T1 (2); <br/> Aclass T2; <br/> T2 = test (T1 ); <br/> t1.print (); <br/> t2.print (); </P> <p >}< br/>

The result is

Constructor 2
Constructor 0
Constructor 4
Print 4
Print 2
Destructor 3 (the parameter object's destruction has gone)
Destructor 3
Print 2
Print 3
Destructor 3
Destructor 2
Press any key to continue

The real parameter destructor referenced by the parameter is missing.

Who is the extra destructor?

When using breakpoint debugging, we can clearly see the corresponding destructor of the constructor.

Let's take a look at the next example.

# Include <iostream> <br/> using namespace STD; </P> <p> class Aclass <br/>{< br/> Public: <br/> int; <br/> Aclass (int t = 0) {<br/> This-> A = T; <br/> cout <"constructor" <A <Endl; <br/>}< br/> void print () {<br/> cout <"print" <this-> A <Endl; <br/>}< br/> ~ Aclass () {<br/> cout <"destructor" <A <Endl; <br/>}</P> <p> }; </P> <p> void test (Aclass & para) <br/>{< br/> Aclass TMP (4); <br/> TMP. A = para. A + 1; <br/>}< br/> void main () <br/>{< br/> Aclass T1 (2); <br/> Aclass T2; <br/> test (T1); <br/>}

Result

Constructor 2
Constructor 0
Constructor 4
Destructor 3
Destructor 0
Destructor 2
Press any key to continue

Consistent quantity

The difference mainly lies in the return statement.

The problem is probably located here.

We assume that when a function returns a result, we first copy the local variable to be returned.

Then, the return value is assigned to the assigned variable.

TO BE PROVED

 

 

# Include <iostream> <br/> using namespace STD; </P> <p> class Aclass <br/>{< br/> Public: <br/> int; <br/> Aclass (int t = 0) {<br/> This-> A = T; <br/> cout <"constructor" <A <Endl; <br/>}< br/> void print () {<br/> cout <"print" <this-> A <Endl; <br/>}< br/> ~ Aclass () {<br/> cout <"destructor" <A <Endl; <br/>}</P> <p> }; </P> <p> Aclass * test (Aclass para) <br/>{< br/> Aclass TMP (4); <br/> TMP. print (); <br/> TMP. A = para. A + 1; <br/> para. print (); <br/> return & TMP; </P> <p >}< br/> void main () <br/>{< br/> Aclass T1 (2); <br/> Aclass * t2; <br/> T2 = test (T1 ); <br/> t1.print (); <br/> T2-> Print (); </P> <p>}

The result is

Constructor 2
Constructor 4
Print 4
Print 2
Destructor 3
Print 2
Print-858993460
Destructor 2
Press any key to continue

The pointer of a temporary object cannot be returned.

The object pointer is called by constructors because the object memory is not allocated.

 

# Include <iostream> <br/> using namespace STD; </P> <p> class Aclass <br/>{< br/> Public: <br/> int; <br/> Aclass (int t = 0) {<br/> This-> A = T; <br/> cout <"constructor" <A <Endl; <br/>}< br/> void print () {<br/> cout <"print" <this-> A <Endl; <br/>}< br/> ~ Aclass () {<br/> cout <"destructor" <A <Endl; <br/>}</P> <p> }; </P> <p> Aclass test (Aclass & para) <br/>{< br/> Aclass * TMP = new Aclass (4 ); <br/> TMP-> A = para. A + 1; </P> <p> return * TMP; <br/>}< br/> void main () <br/>{< br/> Aclass T1 (2); <br/> Aclass T2; <br/> T2 = test (T1 ); </P> <p>}

If new is used to dynamically allocate memory, the Destructor is called only when Delete is used.

Dynamic variables exist in heap memory and must be manually released

There must be a delete corresponding to a new 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.