Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze C++ copy constructor in depth and rewrite operator

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to carry out in-depth analysis of C++ copy constructor and rewrite operator, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Class CTestCopyConstruct {public: CTestCopyConstruct () {TRACE ("Enter CTestCopyConstruct (); this is% DBO", this); strTest = "not ok"; I = 0;} CTestCopyConstruct (const CTestCopyConstruct & src) {TRACE ("Enter CTestCopyConstruct (const CTestCopyConstruct & src); this is% dtsmsc is% DSEO", this, & src); strTest = src.strTest;i = src.i } CTestCopyConstruct & operator = (const CTestCopyConstruct & src) {TRACE ("Enter CTestCopyConstruct & operator = (const CTestCopyConstruct & src); this is% Dtinct SRC is% DAPO", this, & src); strTest = src.strTest;i = src.i; return * this;} CString strTest; int i;}

CTestCopyConstruct GetTest () {

CTestCopyConstruct ret1

Ret1.strTest = "ok"

Ret1.i = 0

CTestCopyConstruct ret2

Return ret1

}

Void CTestDlg::OnOK ()

{

CTestCopyConstruct var1

CTestCopyConstruct var2 = GetTest ()

TRACE ("/ nresult 1porpora")

TRACE ("var1 is% DSPO", & var1)

TRACE ("var2 is d var2.str is s/n/n", & var2,var2.strTest)

CTestCopyConstruct var3 = var2

CTestCopyConstruct var4

Var4 = var2

TRACE ("/ nresult 2mono")

TRACE ("var3 is d var3.str is% SPO", & var3,var3.strTest)

TRACE ("var4 is d var2.str is% SPO", & var4,var4.strTest)

}

The code is as above, and the debug window output is as follows:

Enter CTestCopyConstruct (); this is 1242980

Enter CTestCopyConstruct (); this is 1242848

Enter CTestCopyConstruct (); this is 1242840

Enter CTestCopyConstruct (const CTestCopyConstruct & src); this is 1242972 SRC is 1242848

Result 1:

Var1 is 1242980

Var2 is 1242972 var2.str is ok

Enter CTestCopyConstruct (const CTestCopyConstruct & src); this is 1242964 [SRC is 1242972]

Enter CTestCopyConstruct (); this is 1242956

Enter CTestCopyConstruct & operator = (const CTestCopyConstruct & src); this is 1242956 is SRC is 1242972

Result 2:

Var3 is 1242964 var3.str is ok

Var4 is 1242956 var2.str is ok

Analysis:

CTestCopyConstruct var1;//1

CTestCopyConstruct var2 = GetTest (); / / 2

The code is executed as follows:

Current stack pointer (sp) = 1242980

Sp-= 8max / allocate space for var1 in the stack

Call the constructor on var1 (1242980-1242973)

Sp-= 8max / allocate space for var2 in the stack

Sp-= njump / protect the current environment

Entered the GetTest function

Current sp = 1242848

Sp-= 8pm / allocate space for ret1

Build ret1

Sp-= 8pm / allocate space for ret2

Build ret2

.

Call the copy constructor on var2 (stack segment at 1242972), taking test1 (1242848) as an argument

/ / deconstructing test1 test2, etc.

Sp + = njump / restore the operating environment

.

In addition:

Operater = (), unlike the default constructor, only overrides the = operator without providing a copy constructor, and the default constructor is still called.

The default constructor does not handle the same situation as the assignment operator, one on the allocated space and the other on the constructed object.

The default copy constructor calls the copy constructor of each member of the class. CString provides a copy constructor, so even if the copy constructor is removed in the above example, var2 will still get the correct value.

The environment for debugging is the default option for vc6.0 debug. The compilation is not optimized.

CTestCopyConstruct (const CTestCopyConstruct & src) {TRACE ("Enter CTestCopyConstruct (const CTestCopyConstruct & src); this is% DX SRC is% DBO", this, & src); strTest = src.strTest;i = src.i;}

Vs.

CTestCopyConstruct (const CTestCopyConstruct & src)

: strTest (src.strTest) {

TRACE ("Enter CTestCopyConstruct (const CTestCopyConstruct & src); this is% dittsrc is% Dapo", this, & src)

I = src.i

}

The former first calls CString::CString () and then calls CString::operator =

The latter calls CString::CString (CString & src) directly.

The default behavior of the assignment operation: first invoke the assignment operation of the parent class.

Then it will look for assignment operations for its own unique members. If the member's assignment operator is overridden, the overridden assignment operator function is called, and if the rewritten operator function is private, the compilation will not pass.

The default behavior of the copy constructor: first call the copy constructor of the parent class.

Then look for copy constructors for your own unique members. If this member provides a copy constructor, it is called, and if the copy constructor provided by the member's class is private, the compilation will not pass.

Can a subclass treat the parent class as a member of its own?

It can be said that the default assignment operation and the default copy constructor are the two most commonly used functions of the class. The interior is not as complicated as usual.

On how to carry out C++ copy constructor in-depth analysis and rewrite operator to share here, I hope the above can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report