copy ctor、copy assignment(拷贝构造函数和拷贝赋值函数)

这个很早就搞明白了,只是这里系统整理一下。

例1:

#include<iostream>
using namespace std;
class Test {
public:
	Test(int test_data = 0) :test_data(test_data)
	{
		cout << "ctor" << endl;
	}
	Test(const Test&obj) { cout << "copy ctor" << endl; }
	Test& operator=(const Test&other) {
		cout << "copy assignment" << endl;
		if (this == &other)
		{
			return *this;
		}
		this->test_data = other.test_data;
		return *this;
	}
private:
	int test_data;
};
int main()
{


	Test t1;
	Test t2(t1);//copy ctor
	cout << "-------------" << endl;
	Test t3 = t1;//copy ctor
	cout << "-------------" << endl;
	t3 = t2; //copy assignment

	system("pause");
	return 0;
}

在这里插入图片描述
其实很简单,copy ctor是针对一个未存在的对象进行初始化;copy assignment是针对已存在的对象进行初始化。

例2:

#include<iostream>
#include<functional>
#include<numeric>
using namespace std;


// TEMPLATE STRUCT plus
template<class _Ty = void>
struct my_plus
{	// functor for operator+
	typedef _Ty first_argument_type;
	typedef _Ty second_argument_type;
	typedef _Ty result_type;
	my_plus() { cout << "my_plus ctor" << endl; }
	my_plus(my_plus&obj) { cout << "my_plus copy ctor" << endl; }
	constexpr _Ty operator()(const _Ty& _Left, const _Ty& _Right) const
	{	// apply operator+ to operands
		return (_Left + _Right);
	}
};


template<class _InIt,
	class _Ty,
	class _Fn2> inline
	_Ty my_accumulate(_InIt _First, _InIt _Last, _Ty _Val, _Fn2 _Func)
{	// return sum of _Val and all in [_First, _Last), using _Func
	for (; _First != _Last; ++_First)
		_Val = _Func(_Val, *_First);
	return (_Val);
}

// FUNCTION TEMPLATE accumulate
template<class _InIt,
	class _Ty> inline
	_Ty my_accumulate(_InIt _First, _InIt _Last, _Ty _Val)
{	// return sum of _Val and all in [_First, _Last)
	return (my_accumulate(_First, _Last, _Val, my_plus<_Ty>()));
}
int main() {
	//二元函数对象的使用
	int a[] = { 1,2,3,4,5 };
	const int N = sizeof(a) / sizeof(int);

	cout << my_accumulate(a, a + N, 0, my_plus<int>()) << endl;//15
	cout << "-------------------" << endl;
	my_plus<int> obj;
	cout << my_accumulate(a, a + N, 0, obj) << endl;//15

	system("pause");
	return 0;
}

在这里插入图片描述
这里其实想说的就是函数形参为类的对象,临时对象作为实参传入时的情况,编译器会进行优化,只调用了一次ctor:

#include<iostream>
using namespace std;
class Test {
public:
	Test(int test_data = 0) :test_data(test_data)
	{
		cout << "ctor" << endl;
	}
	Test(const Test&obj) { cout << "copy ctor" << endl; }
	Test& operator=(const Test&other) {
		cout << "copy assignment" << endl;
		if (this == &other)
		{
			return *this;
		}
		this->test_data = other.test_data;
		return *this;
	}
private:
	int test_data;
};

void fun(Test t) {}

int main()
{
	Test t1;
	fun(t1);
	cout << "------" << endl;
	fun(Test());
	system("pause");
	return 0;
}

在这里插入图片描述

例3:

稍作改动,将函数的返回类型也改成类的对象:

#include<iostream>
using namespace std;
class Test {
public:
	Test(int test_data = 0) :test_data(test_data)
	{
		cout << "ctor" << endl;
	}
	Test(const Test&obj) { cout << "copy ctor" << endl; }
	Test& operator=(const Test&other) {
		cout << "copy assignment" << endl;
		if (this == &other)
		{
			return *this;
		}
		this->test_data = other.test_data;
		return *this;
	}
private:
	int test_data;
};

Test fun(Test t) { return t; }

int main()
{
	Test t1;
	fun(t1);
	cout << "------" << endl;
	fun(Test());
	system("pause");
	return 0;
}

显然,当fun执行完毕,返回的时候还会额外增加一次copy ctor的调用。
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值