/*

 * 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. 【java】将字符串的首字母大写

    工具方法: public static void main(String[] args) { System.out.println(upperCaseFirst("barer")) ...

  2. 内存控制篇calloc free getpagesize malloc mmap munmap

    calloc(配置内存空间) 相关函数 malloc,free,realloc,brk 表头文件 #include <stdlib.h> 定义函数 void *calloc(size_t ...

  3. Informatica 常用组件Expression之二 创建EXP组件

    在 Mapping Designer 中选择"转换-创建".选择表达式转换.为它输入一个名称(惯例为 EXP_TransformationName)并单击"确定" ...

  4. LoadTestAgentResultsLateException in VS2010

    遇到报错, 首先得先仔细读读人家给的出错信息. 而不是先怀疑是自己什么地方弄错了, 胡乱修改. 比如说, 遇到下面的报错: LoadTestAgentResultsLateException    R ...

  5. scala 学习笔记八 简洁性

    Scala可以简洁地表示概念,有时甚至可以说过于简洁. 1.消除中间结果 在组合表达式中,最后一个表达式会变成整个表达式的结果.下面的例子中,值会被捕获到val result中,然后result从方法 ...

  6. Projects Plan For Remaining 2013

    Projects Plan For Remaining 2013 - Lisp Project: To Be. - Python Project: To Be. - Perl Project: To ...

  7. Windows10下安装pytorch并导入pycharm

    1.安装Anaconda 下载:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 安装Anaconda3,最新版本的就可以了,我安装的是5. ...

  8. 如何在Linux上安装服务器管理软件Cockpit

    Cockpit 是一个自由开源的服务器管理软件,使得我们可以通过它好看的 Web 前端界面轻松地管理我们的 GNU/Linux 服务器,非常轻量级,Web 界面也非常简单易用. Cockpit 使得 ...

  9. 【HTML5】实现QQ聊天气泡效果

    今天自己用 HTML/CSS 做了个类似QQ的聊天气泡,以下是效果图: 以下说下关键地方的样式设置.然后贴出html和css代码(不多). 步骤1:布局 消息採用div+float布局,每条消息用一个 ...

  10. 老三星手机i9001刷机记录

    家里的老的三星i9001,准备给我妈用,打算刷机,但又实在头疼那些复杂的刷机技术,昨天研究了一下,用比较简单的方法完成刷机,记录如下: 用卡刷比较简单,线刷不考虑 进入恢复模式的方法:1.电源+音量加 ...