从零开始学习c++之拷贝构造函数

拷贝构造函数:

定义:

   如果一个构造函数的第一个参数是自身类类型的引用,且任何额外参数都有默认值,则此构造函数是拷贝构造函数。

典型的拷贝构造函数:

class Foo{
public:
    Foo(){};       //默认构造函数
    Foo(const Foo& f){};      //默认拷贝构造函数
}

拷贝构造函数的第一个参数必须是引用类型,虽然可以定义一个接收非const引用的拷贝构造函数,但此参数几乎总是一个const的引用。


何时使用拷贝构造函数:

(1)拷贝初始化(用=定义变量)

(2)将一个对象作为实参传递给非引用类型的形参

(3)一个返回类型为非引用类型的函数返回一个对象


示例题目:

假如Point是一个类类型,它有一个public的拷贝构造函数,指出下面程序片段中哪些地方使用了拷贝构造函数;

Point global;
Point func(Point p)
{
    Point local = p,*heap = new Point(global);
    *heap = local;
    Point pa[4] = {local,*heap};
    return *heap;
}
解答:

(1)local = p,将p拷贝给local

(2)*heap = local,将local拷贝给heap指定的地址

(3)Point pa[4] = {local,*heap};    ,将local和*heap拷贝给数组的前两位元素

(4)return *heap;

示例代码:

#include <iostream>
#include <string>
using namespace std;

static int objectCount = 0;  //全局变量存储对象数量
class HowMany {
public :
	HowMany() { objectCount++; print("HowMany()"); }      //默认构造函数,并输出
	void print(const string& msg = "") {
		if (msg.size() != 0) cout << msg << ": ";     //输出string类型参数
		cout << "ObjectCount:" << objectCount << endl;    //输出全局变量
	}
	~HowMany() {                        //析构函数
		objectCount--;
		print("~HowMany()");      
	}
};


HowMany f(HowMany x) {
	cout << "Begin of f " << endl;
	x.print("x argument inside of f()");
	cout << "End of f" << endl;
	return x;
}

int main()
{
	HowMany h;
	h.print("After construction of h");
	HowMany h2 = f(h);
	h.print("After call of f()");
	

    return 0;
}
输出结果:

HowMany() : ObjectCount:1                                      //h构造 count++
After construction of h : ObjectCount:1                     //h调用print函数,count == 1
Begin of f
x argument inside of f() : ObjectCount : 1                 //参数x没有调用构造函数,count未变
End of f
~HowMany() : ObjectCount : 0                                  //f函数结束后,却析构了x,count--
After call of f() : ObjectCount : 0
~HowMany() : ObjectCount : -1                                 //h析构
~HowMany() : ObjectCount : -2                                  //h2析构

可以发现:f函数在调用HowMany类型参数x时,没有显性的调用构造函数,所以objectCount没有++,但却析构了x,objectCount--,导致最终结果的Count并没有归零


现在修改示例代码的HowMany类:

class HowMany {
public :
	HowMany() { objectCount++; print("HowMany()"); }      
	HowMany(const HowMany& h) { objectCount++; print("HowMany(h)"); }          //新的拷贝构造函数
	void print(const string& msg = "") {
		if (msg.size() != 0) cout << msg << ": ";     
		cout << "ObjectCount:" << objectCount << endl;    
	}
	~HowMany() {              //析构函数
		objectCount--;
		print("~HowMany()");      
	}
};

运行后结果:

HowMany(): ObjectCount:1                       //h调用构造
After construction of h: ObjectCount:1        
HowMany(h): ObjectCount:2                       //f函数参数x调用构造
Begin of f
x argument inside of f(): ObjectCount:2
End of f
HowMany(h): ObjectCount:3                         //h2调用构造
~HowMany(): ObjectCount:2
After call of f(): ObjectCount:2
~HowMany(): ObjectCount:1
~HowMany(): ObjectCount:0
此时,h,h2,和参数x分别调用了我们所写构造函数,增加了count,并在最后析构了自身。


在C++中,如果没有写默认的拷贝构造函数,编译器会自动调用默认的拷贝构造函数,默认的拷贝构造函数会作成员变量的拷贝,拷贝对应的类型来构造,并递归的进行下去。所以如果构造函数的参数不是引用类型,此时构造函数会无限循环,所以拷贝构造函数的第一个参数必须是引用类型。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值