zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    UE4免费了
    c# 下 根据Datatable的结构动态创建表
    SharePoint 调用 WebService操作List小记
    同步SharePoint List数据到关系数据库
    如何提升SharePoint 2010的性能
    DataX的使用
    C#调用RabbitMQ实现消息队列
    在SharePoint2010上使用QuickFlow和QuickFlowDesigner
    QUICKFLOW安装
    如何设置IIS程序池的回收时间,才能最大程度的减少对用户的影响?
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5352896.html
Copyright © 2011-2022 走看看