C拷貝構(gòu)造函數(shù)的幾個知識點總結(jié)及示例代碼_第1頁
C拷貝構(gòu)造函數(shù)的幾個知識點總結(jié)及示例代碼_第2頁
C拷貝構(gòu)造函數(shù)的幾個知識點總結(jié)及示例代碼_第3頁
C拷貝構(gòu)造函數(shù)的幾個知識點總結(jié)及示例代碼_第4頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

C++拷貝構(gòu)造函數(shù)的知識點總結(jié)一拷貝構(gòu)造函數(shù)是C++最基礎(chǔ)的概念之一,大家自認(rèn)為對拷貝構(gòu)造函數(shù)了解么?請大家先回答一下三個問題:1.

以下函數(shù)哪個是拷貝構(gòu)造函數(shù),為什么?X::X(const

X&);

X::X(X);

X::X(X&,

int

a=1);

X::X(X&,

int

a=1,

b=2);

2.

一個類中可以存在多于一個的拷貝構(gòu)造函數(shù)嗎?3.

寫出以下程序段的輸出結(jié)果,并說明為什么?

如果你都能回答無誤的話,那么你已經(jīng)對拷貝構(gòu)造函數(shù)有了相當(dāng)?shù)牧私狻?include

#include

struct

X

{

template<typename

T>

X(

T&

)

{

std::cout

<<

"This

is

ctor."

<<

std::endl;

}

template<typename

T>

X&

operator=(

T&

)

{

std::cout

<<

"This

is

ctor."

<<

std::endl;

}

};

void

main()

{

X

a(5);

X

b(10.5);

X

c

=

a;

c

=

b;

}

解答如下:1.

對于一個類X,如果一個構(gòu)造函數(shù)的第一個參數(shù)是下列之一:

a)X&

b)

constX&

c)volatileX&

d)

constvolatileX&

且沒有其他參數(shù)或其他參數(shù)都有默認(rèn)值,那么這個函數(shù)是拷貝構(gòu)造函數(shù).

X::X(const

X&);

//是拷貝構(gòu)造函數(shù)

X::X(X&,

int=1);

//是拷貝構(gòu)造函數(shù)

2.類中可以存在超過一個拷貝構(gòu)造函數(shù),

class

X

{

public:

X(const

X&);

X(X&);

//

OK

};

注意,如果一個類中只存在一個參數(shù)為X&的拷貝構(gòu)造函數(shù),那么就不能使用constX或volatileX的對象實行拷貝初始化.class

X

{

public:

X();

X(X&);

};

const

X

cx;

X

x

=

cx;

//

error

如果一個類中沒有定義拷貝構(gòu)造函數(shù),那么編譯器會自動產(chǎn)生一個默認(rèn)的拷貝構(gòu)造函數(shù).

這個默認(rèn)的參數(shù)可能為X::X(constX&)或X::X(X&),由編譯器根據(jù)上下文決定選擇哪一個.默認(rèn)拷貝構(gòu)造函數(shù)的行為如下:

默認(rèn)的拷貝構(gòu)造函數(shù)執(zhí)行的順序與其他用戶定義的構(gòu)造函數(shù)相同,執(zhí)行先父類后子類的構(gòu)造.

拷貝構(gòu)造函數(shù)對類中每一個數(shù)據(jù)成員執(zhí)行成員拷貝(memberwise

Copy)的動作.

a)如果數(shù)據(jù)成員為某一個類的實例,那么調(diào)用此類的拷貝構(gòu)造函數(shù).

b)如果數(shù)據(jù)成員是一個數(shù)組,對數(shù)組的每一個執(zhí)行按位拷貝.

c)如果數(shù)據(jù)成員是一個數(shù)量,如int,double,那么調(diào)用系統(tǒng)內(nèi)建的賦值運算符對其進(jìn)行賦值.

3.

拷貝構(gòu)造函數(shù)不能由成員函數(shù)模版生成.

struct

X

{

template<typename

T>

X(

const

T&

);

//

NOT

copy

ctor,

T

can't

be

X

template<typename

T>

operator=(

const

T&

);

//

NOT

copy

ass't,

T

can't

be

X

};

原因很簡單,成員函數(shù)模版并不改變語言的規(guī)則,而語言的規(guī)則說,如果程序需要一個拷貝構(gòu)造函數(shù)而你沒有聲明它,那么編譯器會為你自動生成一個.所以成員函數(shù)模版并不會阻止編譯器生成拷貝構(gòu)造函數(shù),賦值運算符重載也遵循同樣的規(guī)則.(參見Effective

C++3edition,Item45)

二針對上面作者的討論,理解更深了,但是下面我還是會給出一個一般的標(biāo)準(zhǔn)的實現(xiàn)和注意事項:#include

"stdafx.h"

#include

"stdio.h"

#include

<iostream>

#include

<string>

struct

Test1

{

Test1()

{

}

Test1(int

i)

{

id

=

i;

}

Test1(const

Test1&

test)

{

id

=

test.id;

}

Test1&

operator

=

(const

Test1&

test)

{

if(this

==

&test)

return

*this;

id

=

test.id;

return

*this;

}

int

id;

};

class

Test2

{

public:

Test2(){

m_pChar

=

NULL;}

Test2(char

*pChar)

{

m_pChar

=

pChar;}

Test2(int

num)

{

m_pChar

=

new

char[num];

for(int

i

=

0;

i<

num;

++i)

m_pChar[i]

=

'a';

m_pChar[num-1]

=

'\0';

}

Test2(const

Test2&

test)

{

char

*pCharT

=

m_pChar;

m_pChar

=

new

char[strlen(test.m_pChar)];

strcpy(m_pChar,

test.m_pChar);

if(!pCharT)

delete

[]pCharT;

}

Test2&

operator

=

(const

Test2&

test)

{

if(this

==

&test)

return

*this;

char

*pCharT

=

m_pChar;

m_pChar

=

new

char[strlen(test.m_pChar)];

strcpy(m_pChar,

test.m_pChar);

if(!pCharT)

delete

[]pCharT;

return

*this;

}

private:

char

*m_pChar;

};

int

main(int

argc,

char*

argv[])

{

const

Test1

ts(1);

//

Test1()

const

Test1*

p_ts

=

&ts;

const

Test1

ts2(ts);

//Test(const

Test1&

test)

const

Test1

ts3

=

ts;

//Test(const

Test1

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論