/*

 * 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. jQuery元素属性attr设置多个键值或函数 删除属性removeAttr

    $("Element").attr(name) '取得第一个匹配的属性值,比如$("img").attr("src") $("El ...

  2. UVA 10026 Shoemaker&#39;s Problem

    Shoemaker's Problem Shoemaker has N jobs (orders from customers) which he must make. Shoemaker can w ...

  3. iOS开发-xCode代码托管到GitHub

    xCode默认的是由源代码管理工作Git,Android Studio内置的也有,之前写过两篇关于Window托管Android代码到GitHub的文章,一直想写篇关于Mac上托管代码的到文章,今天终 ...

  4. Facade 门面模式 封装 MD

    门面模式 简介 作用:封装系统功能,简化系统调用 门面模式要求一个系统的外部与其内部的通信必须通过一个统一的门面(Facade)对象进行.门面模式提供一个高层次的接口,使得系统更易于使用. 门面模式的 ...

  5. AngularJs 阻止事件运行,防止冒泡穿透事件

    ng-click 低啊用方法后 添加语句$event.stopPropagation(); <button type="button" ng-click="doSo ...

  6. 用C#代码编写的SN快速输入工具

    一般软件都要输入序列号(SN),而大家平时用的最多的恐怕是盗版软件,通常盗版软件的序列号(SN)都保存成:XXXXX-XXXXX-XXXX-XXXX的形式. 而软件输入序列号的地方通常都是几个文本框( ...

  7. HDU 5411 CRB and Puzzle (2015年多校比赛第10场)

    1.题目描写叙述:pid=5411">点击打开链接 2.解题思路:本题实际是是已知一张无向图.问长度小于等于m的路径一共同拥有多少条. 能够通过建立转移矩阵利用矩阵高速幂解决.当中,转 ...

  8. (算法)Partition方法求数组第k大的数

    如题,下面直接贴出代码: #include <iostream> using namespace std; int Partition(int* A,int left,int right) ...

  9. MYSQL 表中汉字写入或字段赋值时乱码情况排误

    -- 当改动字段值.或是直接写入时,汉字变成乱码情况 .[可注意一下数据库名,记得改动] -- 当字符顺序对汉字不兼容时,可能直接导致乱码情况发生. 最好做到库.表.字段(字符类型)排序规则是否一致 ...

  10. swift语言实现单例模式

    Swift实现单例模式 单例模式在各个语言中都有实现,swift语言推出已经几天了.通过这几天的看文档.特奉上写的Swift的单例实现,供大家学习交流,欢迎指正. ---若转载请注明出处,本人Gith ...