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个理由,它们分为 ...
随机推荐
- Unity3d操作的一些技巧知识点和BUG解决方案
自己记录一些东西,转载请良心注明出处. 1.如何同时打开两个UNITY3D项目. 有时候需要对比,或者需要添加另一个项目的某资源到目前项目,同时打开两个项目看起来会比较明了.如果直接打开的话, ...
- Android图片加载框架最全解析(五),Glide强大的图片变换功能
大家好,又到了学习Glide的时间了.前段时间由于项目开发紧张,再加上后来又生病了,所以停更了一个月,不过现在终于又可以恢复正常更新了.今天是这个系列的第五篇文章,在前面四篇文章的当中,我们已经学习了 ...
- coursera课程Text Retrieval and Search Engines之Week 2 Overview
Week 2 OverviewHelp Center Week 2 On this page: Instructional Activities Time Goals and Objectives K ...
- 硬件负载均衡F5和软负责均衡Nginx
请直接搜索相关文章了解:http://www.ideadata.com.cn/wisdomAction/readWisdom.do?id=75 F5,硬件 优点:能够直接通过智能交换机实现,处理能 ...
- uva 1025 A Spy in the Metro 解题报告
A Spy in the Metro Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug Secr ...
- 纯净版xp系统在局域网共享需要密码如何解决
纯净版xp系统在局域网共享需要密码怎么办?这是近来不少朋友都向小编反馈的问题.他们表示每次共享者更改密码后其他人都需要再重新输入密码,感觉十分的麻烦.下面是系统城小编给大家整理的一些有关XP系统局域网 ...
- C++中对Mysql的操作函数可以参考以下blog中的内容
http://www.cnblogs.com/lovebread/archive/2009/11/24/1609936.html
- go语言基础之不要操作没有合法指向的内存
1.不要操作没有合法指向的内存 示例: package main //必须有个main包 import "fmt" func main() { //没有指向内存 var p *in ...
- Android数据展示之ListView
Android应用程序中经常需要使用ListView展示数据,一个ListView通常将数据填充到布局文件中,在填充的过程中需要知道连接数据与ListView的适配器.适配器是一个连接数据和Adapt ...
- Android消息通知-Notification
Android中常用的消息提醒,一种是Toast弹出提醒内容,一种是AlterDialog弹出框来提醒用户,还有一种就是消息通知的,用Android经常收到各种通知就是Notifation.Notif ...