Move semantics(C++11)
/*
* Compile with:
* g++ move_test.c -o move_test -std=c++11 -g -fno-elide-constructors
* -fno-elide-constructors disabled the return value optimize.
*/
#include <iostream>
#include <utility>
class A {
public:
A(void)
{
a = new int;
std::cout << "Constructing(normal) A" << (void *)this << std::endl;
}
A(const class A &other)
{
std::cout << "Constructing(copy) A" << (void *)this << std::endl;
}
A(class A &&other)
{
std::cout << "Constructing(move) A" << (void *)this << std::endl;
a = other.a;
other.a = NULL;
}
~A(void)
{
std::cout << "Destructing A" << (void *)this << std::endl;
delete a;
}
void set(int i)
{
*a = i;
}
int get(void)
{
return *a;
}
private:
int *a;
};
class B {
private:
class A a;
public:
B(void)
{
std::cout << "Constructing(normal) B" << (void *)this << std::endl;
}
B(const class B &other)
: a(other.a)
{
std::cout << "Constructing(copy) B" << (void *)this << std::endl;
}
B(class B &&other)
:a(std::move(other.a))
{
std::cout << "Constructing(move) B" << (void *)this << std::endl;
}
~B(void)
{
std::cout << "Destructing B" << (void *)this << std::endl;
}
void set(int i)
{
a.set(i);
}
int get(void)
{
a.get();
}
};
class B func(void)
{
class B b;
b.set(23);
std::cout << "function Seperating..." << std::endl;
std::cout << b.get() << std::endl;
return b;
}
int main(void)
{
class B b(func());
std::cout << b.get() << std::endl;
b.set('w');
std::cout << "Seperating..." << std::endl;
std::cout << b.get() << std::endl;
return 0;
}
Running results:
Constructing(normal) A0xbf965d1c
Constructing(normal) B0xbf965d1c
function Seperating...
23
Constructing(move) A0xbf965d4c
Constructing(move) B0xbf965d4c
Destructing B0xbf965d1c
Destructing A0xbf965d1c
Constructing(move) A0xbf965d48
Constructing(move) B0xbf965d48
Destructing B0xbf965d4c
Destructing A0xbf965d4c
23
Seperating...
119
Destructing B0xbf965d48
Destructing A0xbf965d48
Move semantics(C++11)的更多相关文章
- C++11的value category(值类别)以及move semantics(移动语义)
转载请保留以下声明 作者:赵宗晟 出处:http://www.cnblogs.com/zhao-zongsheng/p/value_categories_and_move_semantics.html ...
- c++11 移动语义move semantics
performance, expensive object copies move semantics, temporary objects implemented with rvalue refer ...
- C++11之 Move semantics(移动语义)(转)
转https://blog.csdn.net/wangshubo1989/article/details/49748703 按值传递的意义是什么? 当一个函数的参数按值传递时,这就会进行拷贝.当然,编 ...
- C++11新特性之 Move semantics(移动语义)
https://blog.csdn.net/wangshubo1989/article/details/49748703 这篇讲到了vector的push_back的两种重载版本,左值版本和右值版本.
- 右值引用和std::move函数(c++11)
1.对象移动 1)C++11新标准中的一个最主要的特性就是移动而非拷贝对象的能力 2)优势: 在某些情况下,从旧内存拷贝到新内存是不必要的,此时对对象进行移动而非拷贝可以提升性能 有些类如IO类或un ...
- C++11的new concepts (move semantic)
MoveConstructible 和MoveAssignable MoveConstructible Specifies that an instance of the type can be mo ...
- C++11 move语意
C++11带来的move语义 C++11引入了move语义,stl中的容器基本都支持move语义,因此我们在使用stl中的容器的时候,就已经使用过move语义了,在网上看了不少关于mo ...
- 【C/C++】C++11 Move, Forward
左值与右值 Lvalue:可以出现在 operator= 左边的 Rvalue:只能出现在operator= 右边的 ; int a = b; a = b; a = a + b; a + b = a; ...
- 推荐使用C++ 11
如果你的代码工作正常并且表现良好,你可能会想知道为什么还要使用C++ 11.当然了,使用用最新的技术感觉很好,但是事实上它是否值得呢? 在我看来,答案毫无疑问是肯定的.我在下面给出了9个理由,它们分为 ...
随机推荐
- 页面中基于JSTL标签调用函数--之${fn:}内置函数
调用这样一个头文件<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions " ...
- 3D屏保: 线圈
LineFlower3DSP 一个3D屏保程序,算法的原理类似于圆内轮旋线的生成. 下载地址: http://files.cnblogs.com/WhyEngine/LineFlower3D_sp.z ...
- JSTORM使用笔记
安装部署 zeromq 简单快速的传输层框架,安装如下: wget http://download.zeromq.org/zeromq-2.1.7.tar.gztar zxf zeromq-2.1.7 ...
- java中需要用equals来判断两个字符串值是否相等
在C++中,两个字符串比较的代码可以为: (string1==string2) 但在java中,这个代码即使在两个字符串完全相同的情况下也会返回false Java中必须使用string1.equal ...
- SQL Server AlwaysOn Setup Step-By-Step Guide
Step-By-Step: Creating a SQL Server 2012 AlwaysOn Availability Group http://blogs.technet.com/b/cani ...
- 【Qt】仿QQ表情选择控件
表情选择控件在聊天应用中常常要用到,做起来尽管不复杂可是非常繁琐.特别是有些图标须要按顺序排列.每次重做必定是非常费时.所以我将聊天表情选择控件封装成一个独立的类QFaceSelectWid ...
- 解决IP地址冲突
1.重新启动路由器就可以. 要是网络上的每一个设备都被分配了动态IP地址,路由器重新启动.又一次分配IP地址给网络上的每一个设备后,这个问题就有望得到解决. 可是假设是在企业内就不可能随便的重新启动公 ...
- js获取checkbox中所有选中值及input后面所跟的文本
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- python使用md5处理下载图片
import urllib2 import hashlib opener = urllib2.build_opener() req = opener.open("http://avatar. ...
- (字符串)最长公共字串(Longest-Common-SubString,LCS)
题目: 给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa].二者的最长公共子串为[aba],长度为3. 子序列是不要求连续的,字串必须是连续的. 思路与代码: 1.简 ...