/*

 * 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)的更多相关文章

  1. C++11的value category(值类别)以及move semantics(移动语义)

    转载请保留以下声明 作者:赵宗晟 出处:http://www.cnblogs.com/zhao-zongsheng/p/value_categories_and_move_semantics.html ...

  2. c++11 移动语义move semantics

    performance, expensive object copies move semantics, temporary objects implemented with rvalue refer ...

  3. C++11之 Move semantics(移动语义)(转)

    转https://blog.csdn.net/wangshubo1989/article/details/49748703 按值传递的意义是什么? 当一个函数的参数按值传递时,这就会进行拷贝.当然,编 ...

  4. C++11新特性之 Move semantics(移动语义)

    https://blog.csdn.net/wangshubo1989/article/details/49748703 这篇讲到了vector的push_back的两种重载版本,左值版本和右值版本.

  5. 右值引用和std::move函数(c++11)

    1.对象移动 1)C++11新标准中的一个最主要的特性就是移动而非拷贝对象的能力 2)优势: 在某些情况下,从旧内存拷贝到新内存是不必要的,此时对对象进行移动而非拷贝可以提升性能 有些类如IO类或un ...

  6. C++11的new concepts (move semantic)

    MoveConstructible 和MoveAssignable MoveConstructible Specifies that an instance of the type can be mo ...

  7. C++11 move语意

        C++11带来的move语义     C++11引入了move语义,stl中的容器基本都支持move语义,因此我们在使用stl中的容器的时候,就已经使用过move语义了,在网上看了不少关于mo ...

  8. 【C/C++】C++11 Move, Forward

    左值与右值 Lvalue:可以出现在 operator= 左边的 Rvalue:只能出现在operator= 右边的 ; int a = b; a = b; a = a + b; a + b = a; ...

  9. 推荐使用C++ 11

    如果你的代码工作正常并且表现良好,你可能会想知道为什么还要使用C++ 11.当然了,使用用最新的技术感觉很好,但是事实上它是否值得呢? 在我看来,答案毫无疑问是肯定的.我在下面给出了9个理由,它们分为 ...

随机推荐

  1. arcgis连接excel出现数据库失败 外部数据库驱动程序意外错误

    微软搞事情,删除以下更新就行:win7 KB4041678 KB4041681SERVER 2008 R2 KB4041678 KB4041681WIN10 KB4041676 KB4041691SE ...

  2. 【linux】centos7终端中文显示乱码,命令返回中文乱码

    centos7终端中文显示乱码,命令返回中文乱码 1.查看服务器编码的命令 1.1 echo $LANG 1.2 locale 1.3 查看终端xshell编码 如果以上的三点依旧保持一致,而依旧乱码 ...

  3. 基于非比較的排序:计数排序(countSort),桶排序(bucketSort),基数排序(radixSort)

    计数排序 条件:要排序的数组的元素必须是在一定范围的,比方是1~100.在排序之前我们必须知道数组元素的范围. 思路:顾名思义:就是用一个数组来计数的. 步骤: 1.用一个数组来计数count[ ], ...

  4. 使用 NSPropertyListSerialization 持久化字典与数组

    NSPropertyListSerialization The NSPropertyListSerialization class provides methods that convert prop ...

  5. Unity3d通用工具类之数据配置加载类-ini配置文件加载

    Unity3d通用工具类之数据配置加载类-ini配置文件加载 上次我们讲过xml文件的加载配置管理,今天我们换个配置文件,也是比较常见的配置文件.ini格式的数据. 按照国际管理先贴一张啥是.ini文 ...

  6. openssh-server

    安装 apt-get install openssh-server 配置 sudo gedit /etc/ssh/sshd_config PermitRootLogin without-passwor ...

  7. sudo:抱歉,您必须拥有一个终端来执行 sudo 解决办法;ssh执行sudo命令的方法;给用户增加sudo免密权限

    1.supervisor使用sudo执行命令的时候,报错 2.解决办法: 编辑 /etc/sudoers 文件,将Default requiretty注释掉. sudo vim /etc/sudoer ...

  8. Hibernate @Entity注解配置说明

    持久化是位于JDBC之上的一个更高层抽象. 持久层将对象映射到数据库.以便在查询.装载.更新或删除对象的时候,无须使用像JDBC那样繁琐的API.EJB的早期版本号中.持久化是EJB平台的一部分.EJ ...

  9. 混沌数学之Standard模型

    相关软件混沌数学之离散点集图形DEMO 相关代码: class StandardEquation : public DiscreteEquation { public: StandardEquatio ...

  10. OTL使用指南

    1 OTL简介 OTL 是 Oracle, Odbcand DB2-CLI Template Library 的缩写,是一个C++编译中操控关系数据库的模板库,它目前几乎支持当前所有的各种主流数据库, ...