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个理由,它们分为 ...
随机推荐
- ExtJs 起始日期 结束日期 验证
Ext.apply(Ext.form.VTypes,{ daterange: function(val, field) { var date = field.parseDate(val); // We ...
- chromium对网页获取favicon
每一个网页都有一个favicon,在历史记录的保存中须要用到.在content文件夹下,这个没有实现. 以下说一下我的实现过程: web_contents_impl.cc文件里有方法:WebConte ...
- 日期时间篇asctime ctime gettimeofday gmtime localtime mktime settimeofday time
asctime(将时间和日期以字符串格式表示) 相关函数 time,ctime,gmtime,localtime 表头文件 #include<time.h> 定义函数 char * asc ...
- coursera课程Text Retrieval and Search Engines之Week 4 Overview
Week 4 OverviewHelp Center Week 4 On this page: Instructional Activities Time Goals and Objectives K ...
- CentOS7.0 x86_64系统上构建php开发环境--Lamp(包含设置虚拟文件夹,加入SELinux对httpd的支持等知识)
一.安装mysql,直接用yum安装就可以,mysql在centos7.0版本号中被mariadb替代了. 命令: yum install mysql-server mysql 安装好了,选择改动my ...
- shell more less cat
cat 连续显示.查看文件内容 more 分页查看文件内容 less 分页可控制查看文件内容 通俗点说: cat一次性把文件内容全部显示出来,管你看不看得清,显示完了cat命令就返回了,不能进行交互式 ...
- 解决PHP在Windows IIS 上传的图片无法访问的问题
最近在做一个网站项目遇到了一个很奇怪的问题,现记录下来希望可以帮助到其他的朋友 问题描述: 最近公司刚刚在香港购买了一个Windows Server 2008 服务器用于将一个客户的N个php网站 ...
- 在Hadoop上运行基于RMM中文分词算法的MapReduce程序
原文:http://xiaoxia.org/2011/12/18/map-reduce-program-of-rmm-word-count-on-hadoop/ 在Hadoop上运行基于RMM中文分词 ...
- 微信小程序价值思考:手机端的CS-BS迁移
从很多特点来看,小程序都非常类似于网页:主要的业务逻辑在服务端.客户端无需安装应用程序.小程序的开发采用的HTML+JS+CSS技术等等.张小龙自己对小程序的定位也大概如此:无意做小程序分发平台,只是 ...
- 【pyhon】理想论坛爬虫1.05版,将读取和写DB分离成两个文件
下午再接再厉仿照Nodejs版的理想帖子爬虫把Python版的也改造了下,但美中不足的是完成任务的线程数量似乎停滞在100个左右,让人郁闷.原因还待查. 先把代码贴出来吧,也算个阶段性成果. 爬虫代码 ...