(David reading notes) Construct and Destruct on objects

xiaoxiao2021-03-06  74

David

:

Today, organize the old information on Computer

I found two articles that I didn't know when I didn't know when I came from the Internet.

Talking about the problem of object construct and destruct

I feel that the author is not too clear.

, Released this after rewriting according to its thoughts

.

The object's Construct and Destruct are C

one of the most basic concepts

Although it is simple

But some of these features are also worthy of our attention.

In order to make better use

, Write effective and efficient code

.

Look at a program first

The program is very simple

, Just add some output information to make it look a bit more

:

// Demonstrates Returning a Temporary Object.

#include

Using Namespace STD

;

// Class to Print Construct / Destruct Information

Class CDEMO

{

public

:

CDEMO

(

Const CDEMO

& Rcd

) {

cout

<<

Copy Construction: "

<< & rcd

<<

"->"

<<

THIS

<< ENDL

}

CDEMO

() {

cout

<<

Default Constructionor: "

<<

THIS

<< ENDL

}

CDEMO

&

Operator

=

Const CDEMO

& Rcd

) {

cout

<<

"Assignment Operator:"

<< & rcd

<<

"->"

<<

THIS

<< ENDL

;

Return

*

THIS

}

CDEMO

:: ~ cdemo

() {

cout

<<

DESTRUCTOR: "

<<

THIS

<< ENDL

;}};

// demo function to use cdemo and trigger something under the hoodcdemo foo

() {

cout

<<

"In foo"

<< ENDL

;

CDEMO CD

;

cout

<<

"CD IS:"

<< & CD

<< ENDL

;

// Return A Copy Of CD.

Return CD

}

int

main

() {

// this code generates a Temporary // Object, Which is copied // @ ip2 using the assignment // Operator. CDEMO CD2

;

CD2

= Foo

();

cout

<<

"CD2 IS:"

<< & cd2

<< ENDL

<< ENDL

;

// this code genereates the temporary // Object Directly in the location // of return; cdemo CD1

= Foo

();

cout

<<

"CD1 IS:"

<< & cd1

<< ENDL

<< ENDL;

Const CDEMO

& Rcd

= Foo

();

cout

<<

"Return from main!"

<< ENDL

;

Return

0

}

The following is the output of the program

:

Default Constructionor

: 0012FF6C

In foo

Default Constructionor

: 0012FEEC

CD IS

: 0012FEEC

Copy Constructor

: 0012FEEC

-> 0012FF60

Destructor

: 0012FEEC

Assignment

Operator

: 0012FF60

-> 0012FF6C

Destructor

: 0012FF60

CD2 IS

: 0012FF6C

In foo

Default Constructionor

: 0012FEEC

CD IS

: 0012FEEC

Copy Constructor

: 0012FEEC

-> 0012FF70

Destructor

: 0012FEEC

CD1 IS

: 0012FF70

In foo

Default Constructionor

: 0012FEEC

CD IS

: 0012FEEC

Copy Constructor

: 0012FEEC

-> 0012FF64

Destructor

: 0012FEEC

Return from

main

!

Destructor

: 0012FF64

Destructor

: 0012FF6C

Destructor

: 0012FF70

In the above program

MAIN function

We define two CDEMO objects and a reference to a CDEMO object in different ways.

Obviously

Different ways

The above output has a big difference

.

The following is one pair one pair

3 sets of outputs analysis

Please pay attention to the address information in the output information.

.

1.

CDEMO CD2

;

// Default constructor CD2

= Foo

();

// Economical to experience a default structure, a copy structure (temporary object when the construction is returned), one assignment

2.

CDEMO CD1

= Foo

();

// After a texture and a copy construction process, it seems that there is no process of copying the temporary object returned, is not the case. Due to the optimization of the compiler, the object is directly constructed on the buffer of the CD1.

3.

Const CDEMO

& Rcd

= Foo

();

// This is also a process of copying the temporary object when the configuration is returned, but is different from 1 that the temporary object is not released immediately, but until the main function is returned. Why? In fact, this It is caused by the characteristics of Const Reference, and the C standard is specified. If a temporary object is assigned to a (constant) reference, this temporary object will not be destroyed in this (constant) quote (C standard only) Const Reference is like this, for ordinary Reference, although it is possible, but is not safe).

转载请注明原文地址:https://www.9cbs.com/read-93306.html

New Post(0)