Copy Constructor Function

       拷贝构造函数:

       拷贝构造函数是一种特殊的构造函数,函数的名称必须和类名称一致,它必须的一个参数是本类型的一个引用类型(为了避免递归调用)。它的作用就是用来复制对象的,在使用这个对象的实例来初始化这个对象的一个新的实例。如下示例:

class cbase
{
public:
	cbase()
	{
		value = 0;
		cout<<&value<<" cbase construtor..."<<endl;
	}
	cbase(int val)
	{
		value = val;
		cout<<&value<<" cbase(int) construtor..."<<endl;
	}
	cbase(const cbase & b)
	{
		value = b.value;
		cout<<&value<<" copy constructor..."<<endl;
	}
	~cbase()
	{
		cout<<&value<<" destructor..."<<endl;
	}
private:
	int value;
};

void show_value(cbase b)   //showVaule(cbase &b)
{
}

cbase create_cbase()
{
	cbase base(56);
	return base;
}

int _tmain(int argc, _TCHAR* argv[])
{
	cbase base;
	cbase base2(1);
	cout<<endl;

	cbase base3(base2);
	cbase base4 = base3;
	show_value(base4);
	cout<<endl;

	create_cbase();
	cout<<endl;

	return 0;
}

输出如下:


    浅拷贝与深拷贝:

    1. 默认拷贝构造函数
    很多时候在我们都不知道拷贝构造函数的情况下,传递对象给函数参数或者函数返回对象都能很好的进行,这是因为编译器会给我们自动产生一个拷贝构造函数,这就是“默认拷贝构造函数,这个构造函数很简单,仅仅使用“老对象”的数据成员的值对“新对象”的数据成员一一进行赋值。

如下示例:

class Rect
{
public:
	Rect()
	{
		count++;
	}
// 	Rect(const Rect& r)
// 	{
// 		width=r.width;
// 		height=r.height;   //private mem
// 		count++;
// 	}
	~Rect()
	{
		count--;
	}
	static int getCount()
	{
		return count;
	}
private:
	int width;
	int height;
	static int count;
};

int Rect::count=0;
int main()
{
	Rect rect1;
	cout<<"The count of Rect:"<<Rect::getCount()<<endl;
	Rect rect2(rect1);
	cout<<"The count of Rect:"<<Rect::getCount()<<endl;
	return 0;
}

      这段代码对前面的类,加入了一个静态成员,目的是进行计数。在主函数中,首先创建对象rect1,输出此时的对象个数,然后使用rect1复制出对象rect2,再输出此时的对象个数,按照理解,此时应该有两个对象存在,但实际程序运行时,输出的都是1,反应出只有1个对象。此外,在销毁对象时,由于会调用销毁两个对象,类的析构函数会调用两次,此时的计数器将变为负数。也就是拷贝构造函数没有处理静态数据成员出现这些问题最根本就在于在复制对象时,计数器没有递增,我们重新编写拷贝构造函数如被屏蔽部分即可。

     2. 浅拷贝

     所谓浅拷贝,指的是在对象复制时,只对对象中的数据成员进行简单的赋值,默认拷贝构造函数执行的也是浅拷贝。大多情况下“浅拷贝”已经能很好地工作了,但是一旦对象存在了动态成员,那么浅拷贝就会出问题了。

     3. 防止默认拷贝发生

     通过对对象复制的分析,我们发现对象的复制大多在进行“值传递”时发生,这里有一个小技巧可以防止按值传递——声明一个私有拷贝构造函数。甚至不必去定义这个拷贝构造函数,这样因为拷贝构造函数是私有的,如果用户试图按值传递或函数返回该类对象,将得到一个编译错误,从而可以避免按值传递或返回对象。

参考:http://blog.chinaunix.net/uid-28977986-id-3977861.html





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Here is a sample implementation of the cmplx struct in C++: ```c++ #include<iostream> #include<cmath> using namespace std; struct cmplx { private: double real; double imag; public: cmplx(): real(0), imag(0) {} cmplx(double r, double i = 0): real(r), imag(i) {} cmplx(const cmplx& c): real(c.real), imag(c.imag) {} friend cmplx operator+(const cmplx& c1, const cmplx& c2) { return cmplx(c1.real + c2.real, c1.imag + c2.imag); } friend cmplx operator-(const cmplx& c1, const cmplx& c2) { return cmplx(c1.real - c2.real, c1.imag - c2.imag); } friend cmplx operator*(const cmplx& c1, const cmplx& c2) { return cmplx(c1.real*c2.real - c1.imag*c2.imag, c1.real*c2.imag + c1.imag*c2.real); } friend cmplx operator/(const cmplx& c1, const cmplx& c2) { double d = c2.real*c2.real + c2.imag*c2.imag; return cmplx((c1.real*c2.real + c1.imag*c2.imag) / d, (c1.imag*c2.real - c1.real*c2.imag) / d); } friend cmplx operator+(const cmplx& c, double d) { return cmplx(c.real + d, c.imag); } friend cmplx operator-(const cmplx& c, double d) { return cmplx(c.real - d, c.imag); } friend cmplx operator*(const cmplx& c, double d) { return cmplx(c.real*d, c.imag*d); } friend cmplx operator/(const cmplx& c, double d) { return cmplx(c.real/d, c.imag/d); } friend cmplx operator+(double d, const cmplx& c) { return c + d; } friend cmplx operator-(double d, const cmplx& c) { return cmplx(d) - c; } friend cmplx operator*(double d, const cmplx& c) { return c*d; } friend cmplx operator/(double d, const cmplx& c) { double d2 = c.real*c.real + c.imag*c.imag; return cmplx(d*c.real/d2, -d*c.imag/d2); } friend istream& operator>>(istream& is, cmplx& c) { is >> c.real >> c.imag; return is; } friend ostream& operator<<(ostream& os, const cmplx& c) { os << c.real << " + " << c.imag << "i"; return os; } friend cmplx sq(cmplx& c) { return c*c; } friend cmplx cu(cmplx& c) { return c*c*c; } }; int main() { cmplx a(1, 2), b(3, 4); cmplx c = a + b; cmplx d = a - b; cmplx e = a * b; cmplx f = a / b; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "a + b = " << c << endl; cout << "a - b = " << d << endl; cout << "a * b = " << e << endl; cout << "a / b = " << f << endl; cmplx g = a + 2.5; cmplx h = 3.7 - b; cmplx i = a * 1.5; cmplx j = b / 2.0; cout << "a + 2.5 = " << g << endl; cout << "3.7 - b = " << h << endl; cout << "a * 1.5 = " << i << endl; cout << "b / 2.0 = " << j << endl; cmplx k = 2.5 + a; cmplx l = 3.7 - b; cmplx m = 1.5 * a; cmplx n = 2.0 / b; cout << "2.5 + a = " << k << endl; cout << "3.7 - b = " << l << endl; cout << "1.5 * a = " << m << endl; cout << "2.0 / b = " << n << endl; cout << "a^2 = " << sq(a) << endl; cout << "b^3 = " << cu(b) << endl; return 0; } ``` In this implementation, the cmplx struct has two private member variables for the real and imaginary parts. It also has a default constructor, a user constructor with a default value for the imaginary part, and a copy constructor. The struct defines arithmetic operators (+, -, *, /) for cmplx input and overloaded arithmetic operators for double input. It also defines input and output directives and friend math functions for square and cube.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值