对象被优化以后才是高效的C++编程
对象使用过程中背后调用了哪些方法
函数调用过程中对象背后调用的方法太多
总结三条对象优化的规则
- 函数参数传递过程中,对象优先按引用传递,不要按值传递
- 当函数返回对象的时候,应该优先返回一个临时对象,而不要返 回一个定义过的对象
- 接收返回值是对象的函数调用的时候,优先按初始化的方式接收,不要按赋值的方式接收
用临时对象拷贝构造同类型的新对象时有优化。从上面的 11 行优化成了 4 行
CMyString的代码问题
想法:B 找 A 要东西,如果 A 不要了,A 大可直接把东西给 B,而不是让 B 拷贝一份后, A 把原来的丢掉
当这个东西特别大时,原来的方法需要改变
添加带右值引用参数的拷贝构造和赋值函数
通俗来讲:
左值:有内存、有名字
右值:没名字(临时量)或 没内存
具体例子:用 带右值引用参数的拷贝构造 和 带右值引用参数的赋值重载函数 实现
move移动语义和forward类型完美转发
==采用 move()
将左值强转为右值==
引用折叠:左值加右值为左值;右值加右值为右值 & &&
还是左值引用
智能指针的强大
==深入掌握C++智能指针_大秦坑王_大秦坑王的博客-CSDN博客==