C++ 拷貝構(gòu)造函數(shù)
什么是拷貝構(gòu)造函數(shù)
首先對(duì)于普通類型的對(duì)象來(lái)說(shuō),它們之間的復(fù)制是很簡(jiǎn)單的,例如:
int a = 100;
int b = a;
而類對(duì)象與普通對(duì)象不同,類對(duì)象內(nèi)部結(jié)構(gòu)一般較為復(fù)雜,存在各種成員變量。
下面看一個(gè)類對(duì)象拷貝的簡(jiǎn)單例子。
#include <iostream>
using namespace std;
class CExample {
private:
int a;
public:
//構(gòu)造函數(shù)
CExample(int b)
{ a = b;}
//一般函數(shù)
void Show ()
{
cout<<a<<endl;
}
};
int main()
{
CExample A(100);
CExample B = A; //注意這里的對(duì)象初始化要調(diào)用拷貝構(gòu)造函數(shù),而非賦值
B.Show ();
return 0;
}
運(yùn)行程序結(jié)果為:
100
從以上代碼的運(yùn)行結(jié)果可以看出,系統(tǒng)為對(duì)象 B 分配了內(nèi)存并完成了與對(duì)象 A 的復(fù)制過(guò)程。就類對(duì)象而言,相同類型的類對(duì)象是通過(guò)拷貝構(gòu)造函數(shù)來(lái)完成整個(gè)復(fù)制過(guò)程的。
拷貝構(gòu)造函數(shù)是一種特殊的構(gòu)造函數(shù),它在創(chuàng)建對(duì)象時(shí),是使用同一類中之前創(chuàng)建的對(duì)象來(lái)初始化新創(chuàng)建的對(duì)象??截悩?gòu)造函數(shù)通常用于:
-
通過(guò)使用另一個(gè)同類型的對(duì)象來(lái)初始化新創(chuàng)建的對(duì)象。
-
復(fù)制對(duì)象把它作為參數(shù)傳遞給函數(shù)。
-
復(fù)制對(duì)象,并從函數(shù)返回這個(gè)對(duì)象。
如果在類中沒(méi)有定義拷貝構(gòu)造函數(shù),編譯器會(huì)自行定義一個(gè)。如果類帶有指針變量,并有動(dòng)態(tài)內(nèi)存分配,則它必須有一個(gè)拷貝構(gòu)造函數(shù)??截悩?gòu)造函數(shù)的最常見(jiàn)形式如下:
classname (const classname &obj) { // 構(gòu)造函數(shù)的主體 }
在這里,obj 是一個(gè)對(duì)象引用,該對(duì)象是用于初始化另一個(gè)對(duì)象的。
#include <iostream> using namespace std; class Line { public: int getLength( void ); Line( int len ); // 簡(jiǎn)單的構(gòu)造函數(shù) Line( const Line &obj); // 拷貝構(gòu)造函數(shù) ~Line(); // 析構(gòu)函數(shù) private: int *ptr; }; // 成員函數(shù)定義,包括構(gòu)造函數(shù) Line::Line(int len) { cout << "Normal constructor allocating ptr" << endl; // 為指針?lè)峙鋬?nèi)存 ptr = new int; *ptr = len; } Line::Line(const Line &obj) { cout << "Copy constructor allocating ptr." << endl; ptr = new int; *ptr = *obj.ptr; // copy the value } Line::~Line(void) { cout << "Freeing memory!" << endl; delete ptr; } int Line::getLength( void ) { return *ptr; } void display(Line obj) { cout << "Length of line : " << obj.getLength() <<endl; } // 程序的主函數(shù) int main( ) { Line line(10); display(line); return 0; }
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
Normal constructor allocating ptr Copy constructor allocating ptr. Length of line : 10 Freeing memory! Freeing memory!
下面的實(shí)例對(duì)上面的實(shí)例稍作修改,通過(guò)使用已有的同類型的對(duì)象來(lái)初始化新創(chuàng)建的對(duì)象:
#include <iostream> using namespace std; class Line { public: int getLength( void ); Line( int len ); // 簡(jiǎn)單的構(gòu)造函數(shù) Line( const Line &obj); // 拷貝構(gòu)造函數(shù) ~Line(); // 析構(gòu)函數(shù) private: int *ptr; }; // 成員函數(shù)定義,包括構(gòu)造函數(shù) Line::Line(int len) { cout << "Normal constructor allocating ptr" << endl; // 為指針?lè)峙鋬?nèi)存 ptr = new int; *ptr = len; } Line::Line(const Line &obj) { cout << "Copy constructor allocating ptr." << endl; ptr = new int; *ptr = *obj.ptr; // copy the value } Line::~Line(void) { cout << "Freeing memory!" << endl; delete ptr; } int Line::getLength( void ) { return *ptr; } void display(Line obj) { cout << "Length of line : " << obj.getLength() <<endl; } // 程序的主函數(shù) int main( ) { Line line1(10); Line line2 = line1; // 這里也調(diào)用了拷貝構(gòu)造函數(shù) display(line1); display(line2); return 0; }
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
Normal constructor allocating ptr Copy constructor allocating ptr. Copy constructor allocating ptr. Length of line : 10 Freeing memory! Copy constructor allocating ptr. Length of line : 10 Freeing memory! Freeing memory! Freeing memory!
更多建議: