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 ctorcout << "-------------" << endl;Test t3 = t1;//copy ctorcout << "-------------" << endl;t3 = t2; //copy assignmentsystem("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 operandsreturn (_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 _Funcfor (; _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;//15cout << "-------------------" << endl;my_plus<int> obj;cout << my_accumulate(a, a + N, 0, obj) << endl;//15system("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的调用。
copy ctor、copy assignment(拷贝构造函数和拷贝赋值函数)相关推荐
- 【C++】Big Five: 构造函数、拷贝构造函数、拷贝赋值函数、移动构造函数、析构函数
前言 C++类的成员变量是否含有"指针类型"直接决定了"Big Five"函数(就是标题中的5个函数)的编写!有无指针类型"成员变量造成Big Fiv ...
- C++ 语言拷贝构造函数、拷贝赋值运算符和析构函数
C++ 语言拷贝构造函数.拷贝赋值运算符和析构函数 每个类都定义了一个新类型和在此类型对象上可执行的操作.类可以定义构造函数,用来控制在创建此类型对象时做什么.一个类通过定义五种特殊的成员函数来控制这 ...
- C++拷贝构造函数与拷贝赋值运算符
拷贝构造函数 拷贝构造函数定义了当用同类型的另一个对象初始化新对象时做什么,如果一个构造函数的第一个参数是自身类类型的引用,且任何额外参数都有默认值,则此构造函数是拷贝构造函数.拷贝构造函数的参数类型 ...
- 【C++】对拷贝构造函数 深浅拷贝 的理解 拷贝构造函数作用及用途?什么时候需要自定义拷贝构造函数?
这个题目是参考别人面经的一道题,顺着这个问题研究了一下拷贝构造函数.拷贝构造函数简单点理解就是通过一个已有的对象去构造一个新的对象. class Foo {public:Foo(); //默认构造函数 ...
- C++ 深拷贝和浅拷贝std::move移动构造函数,移动赋值函数
一个class如果包含了普通指针成员的话那就必须实现(如果是智能指针则则没有必要)特别是复制构造函数和移动构造函数不要忘记 构造函数, 析构函数, 拷贝构造函数, 移动构造函数, 移动赋值函数 拷贝复 ...
- 实现数组类(C++ 拷贝构造函数、拷贝函数)要判断赋值左右对象不相等,坑惨了...
#include <iostream> using namespace std; class ArrayIndexOutOfBoundsException{ // 异常类 public:i ...
- C++学习笔记day47-----C++98-继承中的构造函数,析构函数,拷贝构造函数,拷贝赋值函数,多重继承,虚继承
继承中的构造函数 当通过一个子类创建一个新的对象时,编译器会根据子类在初始化表中指明的基类的初始化方式去调用基类相应的构造函数.如果子类的初始化表中,并没有指明基类的初始化方式,编译器将会调用基类的无 ...
- C++ STL笔记二:string容器;string构造函数;string赋值函数;string拼接;string查找、替换;string比较;string单个字符存取;string插入、删除、截取
本质: string是C++风格的字符串,而string本质上是一个类. string和char*区别: char*是一个指针 string是一个类,类内部封装了char*,管理这个字符串,是一个ch ...
- C++ 拷贝构造函数 赋值构造函数
关键字: C++ 默认拷贝构造函数的行为如下: 默认的拷贝构造函数执行的顺序与其他用户定义的构造函数相同,执行先父类后子类的构造. 拷贝构造函数对类中每一个数据成员执行成员拷贝(me ...
最新文章
- JSON Web Token - 在Web应用间安全地传递信息
- 自己动手用C扩展PHP(三)
- 关于J2SE/Jsp/Sping/Hibernate/Struts2的视频下载
- 动态规划--Leetcode746
- 关于SVG的viewBox
- php中::双冒号有什么作用
- 在js、jsp文件中如何获取项目绝对路径
- MySQL 备份 nb3 和 psc的区别
- Solr5.2.1-Cloud-Zookeeper快速搭建
- R语言学习系列(数据挖掘之决策树算法实现--ID3代码篇)
- 【Android studio快捷键】代码提示
- PreScan Regenerate问题
- receptive field
- Zeal 面向开发者的离线文档查看工具
- 启动SpringBoot项目时,报程序包不存在或者找不到符号的错误
- 滁州学院第二期计算机,欢迎加入—学生会(第二期)
- 新建STM32工程报错 warning: At end of source: #12-D: parsing restarts here after previous syntax error
- webstorm最新激活方法。绝对有效
- 永磁同步电机力矩控制(三):关于电机位置信号
- 沧海一粟 之 杏花村
热门文章
- react-router如何配置可选参数
- Odoo隐藏应用模块
- Python爬虫实战爬取租房网站2w+数据-链家上海区域信息(超详细)
- 怎样快速查询单号物流信息,筛选出未签收的单号
- 直板android智能手机,小巧又精悍 3大系统直板全键盘手机搜罗
- 网易互娱2022校园招聘在线笔试-游戏研发工程师(第一批)
- 视频到图片(每隔几帧保存一张图片)opencv实现
- 山西民生云大同员认证在什么网_山西民生云app下载-山西民生云大同app认证下载手机版 v2.2-91优手机网...
- MR(混合现实)无绿幕拍摄
- Hygon C86 7xxx处理器在Windows 10下无法开启虚拟化支持的问题