第八天2017/04/17(1、拷贝构造、❤临时对象)
来源:互联网 发布:华为数据库一体机 编辑:程序博客网 时间:2024/06/16 04:37
调用拷贝构造函数的三种case
1. MyPoint p2 = p1; MyPoint p2(p1); case1中的重点:MyPoint p3 = MyPoint(3,3); //此时:C++编译器进行优化, //只调用一次构造函数(并不涉及拷贝构造函数的调用)2. 形参:类对象,实参:类对象3. 返回值:类对象/*总结与技巧: 一定要区分等号的两种含义:其中一种是拷贝、另外一种是简单赋值。 临时对象析构的时间是遇到主调函数中的第一个分号“;”。*/#include <iostream>using namespace std;class MyPoint{public: MyPoint() { cout<<(int)this<<"构造函数"<<endl; } MyPoint(const MyPoint& obj) { cout<<(int)this<<"拷贝构造函数"<<endl; } MyPoint& operator=(const MyPoint& obj) { cout<<"赋值等号操作"<<endl; return *this; } ~MyPoint() { cout<<(int)this<<"析构函数"<<endl; }};void f1(MyPoint p){}MyPoint f2(){ MyPoint p; return p; } int main(){//case1: MyPoint p1; //构造函数 MyPoint p2 = p1; //MyPoint p2(p1); //拷贝构造函数//case1中的重点 MyPoint p3 = MyPoint(); //【重点】C++编译器进行了优化,只调用——>构造函数//case2: MyPoint qq; //构造函数 f1(qq); //拷贝构造函数tmp、析构tmp//case3:区分下面两种情形,对比分析 qq = f2(); //构造函数p、拷贝构造函数tmp、析构函数p、赋值=、析构函数tmp MyPoint pp = f2(); //构造函数p、拷贝构造函数tmp、析构函数p return 0;}
0 0
- 第八天2017/04/17(1、拷贝构造、❤临时对象)
- object构造、拷贝构造、析构、临时对象
- C++中的临时对象(拷贝构造函数)(上)
- C++中的临时对象(拷贝构造函数)(下)
- C++中的临时对象(拷贝构造函数)(上)
- C++中的临时对象(拷贝构造函数)(下)
- C++中的临时对象(拷贝构造函数)
- 临时对象与拷贝构造函数
- 临时对象与拷贝构造函数
- 返回临时对象时的拷贝构造函数问题
- C++拷贝构造、临时对象、虚拟继承、重载、覆盖、隐藏
- 【题目】C++拷贝构造函数与C++临时对象
- 面对对象第八天
- 第八天:面向对象
- 第八天2017/04/17(2、❤String类的源代码)
- JS学习第八天----对象
- C++对象模型:第2章(2)-拷贝构造函数【深拷贝和浅拷贝】
- 关于C++中在函数中按值返回、临时对象、拷贝构造函数的一点理解
- 关于ViewPager设置属性页setCurrentItem会阻塞主线程ANR总结
- Spring redis listener container配置问题
- 动态规划 02 (最大子矩阵)
- 动态规划:最长上升子序列
- 简单枚举类型——植物与颜色
- 第八天2017/04/17(1、拷贝构造、❤临时对象)
- iOS webView加载本地html、css、js文件
- 常用函数
- 使用phpexcel导出大容量数据时出现的相关问题
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) C. Voltage Keepsake
- 奇舞学院学习笔记之JavaScript一页通
- Lua学习笔记(四)
- 当传入数据只有一个时mybatis中<if>判断会出现There is no getter for property named 'subjectId' in 'class java.lang....
- [BZOJ3179][Coci2012][线段树][贪心]RASPORED