C++11 feature: move constructor
There are heaps of good articles out there about C++ features including this move constructor. However this one focuses on this feature helping resolve a drawback with the C++'s costly value return that has long been annoying me.
Let's have a look at the following code excerpt,
#include <cstring>
#include <utility> class A
{
char *_innerData; public:
A() : _innerData(NULL)
{
} ~A()
{
if (_innerData)
{
delete[] _innerData;
}
} A(char *pszData)
{
int buflen = strlen(pszData)+1;
_innerData = new char[buflen];
strcpy(_innerData, pszData);
} A(const A& other)
{
int buflen = strlen(other._innerData)+1;
_innerData = new char[buflen];
strcpy(_innerData, other._innerData);
} A(A&& other)
{
_innerData = other._innerData;
other._innerData = NULL;
} A& operator=(const A&other)
{
_innerData = new char[strlen(other._innerData)+1];
strcpy(_innerData, other._innerData);
return (*this);
} public:
char *GetData()
{
if (_innerData == NULL) return "<null>";
return _innerData;
} public:
static A ModA(const A &origin)
{
A modified = origin;
char *pOldData = modified._innerData;
modified._innerData = new char[strlen(pOldData)+4];
strcpy(modified._innerData, pOldData);
strcat(modified._innerData, "abc");
delete[] pOldData;
return modified;
}
};
If we call the ModA method as below, normally we expect the value to be copied at least once from the top stack frame to the target value and the copy constructor is invoked.
A a("haha i'm the first");
A d = A::ModA(a);
printf("d = %s\n", d.GetData());
As far as there's a move constructor, the compiler enforces the move constructor being called upon the return of the suburoutine (it makes sense as the local variable will not be used) which makes the value transfer more efficient.
Lets step gack to thinking about the copy constructor. It is inefficient in this case, however it's not impossible to implemebt a shallow copy. However this is not a safe and good pratice as the user might use it assuming it's deep copy and encounter error upon deallocation. That's why move constructor comes in as a memory safe solution.
Note it only works with this pattern, defining a reference type (A& or even A&&) in this case doesn't help and is even invalid, and providing a value through a parameter of a reference type doesn't require this technique.
In general this entire new feature only helps avoid unnecessary deep copy of large internal objects on dynamically allocated memory, it doesn't eliminate copying of statically existing members since it's not a reference/pointer to the whole object.
C++11 feature: move constructor的更多相关文章
- C++11之 Move semantics(移动语义)(转)
转https://blog.csdn.net/wangshubo1989/article/details/49748703 按值传递的意义是什么? 当一个函数的参数按值传递时,这就会进行拷贝.当然,编 ...
- [C++] decltype(auto) C++ 11 feature
1 //C++ 11 feature template <class T1, class T2> auto getMultiply(T1 data1, T2 data2) -> de ...
- stout代码分析之十:c++11之move和forward
stout中大量使用了c++11的特性,而c++11中move和forward大概是最神奇的特性了. 左值和右值的区别 ; // a是左值,0是右值 int b = rand(); // b是左值,r ...
- 右值引用、move与move constructor
http://blog.chinaunix.net/uid-20726254-id-3486721.htm 这个绝对是新增的top特性,篇幅非常多.看着就有点费劲,总结更费劲. 原来的标准当中,参数与 ...
- C++11 std::move和std::forward
下文先从C++11引入的几个规则,如引用折叠.右值引用的特殊类型推断规则.static_cast的扩展功能说起,然后通过例子解析std::move和std::forward的推导解析过程,说明std: ...
- c++11 std::move()
简单点理解,c++11 中的std::move() 函数,实际上就是将一个左值强制转换成一个右值引用数据类型,从而可以调用相应的对右值引用重载的函数. 如果使用std::move() 的返回值做为参数 ...
- c++11 std::move() 的使用
std::move函数可以以非常简单的方式将左值引用转换为右值引用.(左值.左值引用.右值.右值引用 参见:http://www.cnblogs.com/SZxiaochun/p/8017475.ht ...
- 重构改善既有代码设计--重构手法11:Move Field (搬移字段)
你的程序中,某个字段被其所驻类之外的另一个类更多的用到.在目标类建立一个新字段,修改源字段的所有用户,令它们改用新字段. 动机:在类之间移动状态和行为,是重构过程中必不可少的措施.随着系 ...
- C/C++ C++ 11 std::move()
{ 0. C++ 标准库使用比如vector::push_back 等这类函数时,会对参数的对象进行复制,连数据也会复制.这就会造成对象内存的额外创建, 本来原意 是想把参数push_back进去就行 ...
随机推荐
- ssh -v root@xxxxx 显示登录的细节
[root@ok .ssh]# ssh -v root@10.100.2.84 OpenSSH_5.3p1, OpenSSL Feb debug1: Reading configuration dat ...
- MySQL 监控
•Table_locks_immediate The number of times that a request for a table lock could be granted immedia ...
- 《Linux私房菜》笔记和问题记录
鸟哥的Linux私房菜简体首页 对Linux的学习侧重于基本命令和运维相关的部分,最后章节的测试问题不错. 1.VIM程序编辑器 1.所有的Linux都会内建VI:很多软件的编辑接口都会主动呼叫VI: ...
- vim、gvim加载文件慢
1. strace -f -T -o vim.strace vim 2. vim --startuptime "vim-time.txt" 3. gvim -f
- html5 Canvas绘制图形入门详解
html5,这个应该就不需要多作介绍了,只要是开发人员应该都不会陌生.html5是「新兴」的网页技术标准,目前,除IE8及其以下版本的IE浏览器之外,几乎所有主流浏览器(FireFox.Chrome. ...
- Unreal Engine4 学习笔记2 动画蒙太奇
动画蒙太奇出现的位置是在动画蓝图的动画图表和事件图表中,如下图 事件图表,可以看出在主线执行的结尾,如果is Punching 为true,则会执行一个我们自定义的Punch Event,用来播放动画 ...
- web magic 小结
缘起 写了多年的程序,鲜有产出物,于是最近打算做个不可说的东西来祭奠逝去的青春.数据,是一个程序的起点,我们没有数以亿计的用户,无法让活跃用户给我们产生数据,那就只能去别人的站点上借点数据了.这个功能 ...
- Win10 for Phone 裁剪控件
<Page.BottomAppBar> <CommandBar x:Name="appBar"> <AppBarButton Label=" ...
- hdu 3709 数位dp
数位dp,有了进一步的了解,模板也可以优化一下了 题意:找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数例如4139,以3为支点4*2 ...
- ASP.NET 5探险(7):使用混合型控制器方便实现单页应用
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:由于在ASP.NET 5中,MVC和WEB API的技术栈合并了,所以开发混合型Con ...