zoukankan      html  css  js  c++  java
  • c++11新特性之移动语义

    移动语义大大降低了对象转移的成本,通常右值转左值会触发移动语义,左值转左值触发复制语义

    可以看看下面的输出是什么

    #include <iostream>
    #include <string>
    #include <vector>
    class CDemo
    {
    public:
        CDemo()
        {
            //std::cout << "构造函数" << std::endl;
        };
        CDemo(CDemo&& cur) noexcept
        {
            std::cout << "移动函数" << std::endl;
        };
        CDemo(CDemo& cur)
        {
            std::cout << "拷贝函数" << std::endl;
        };
        CDemo& operator=(const CDemo& cur)
        {
            std::cout << "赋值函数" << std::endl;
            return *this;
        }
        ~CDemo()
        {
            //std::cout << "析构函数" << std::endl;
        }
        void print(std::string str)
        {
            std::cout << "CDemo::print "<<str << std::endl;
        }
    };
    CDemo test()
    {
        CDemo demo;
        return demo;
    }
    CDemo test5(CDemo&& demo)
    {
        return demo;
    }
    CDemo test6(CDemo&& demo)
    {
        return demo;
    }
    void test4(CDemo demo)
    {
    }
    void test3(CDemo demo)
    {
    }
    void test2(CDemo demo)
    {
        std::cout << "call test3" << std::endl;
        test3(demo);
    
        std::cout << "call test4" << std::endl;
        test3(std::move(demo));
        return;
    }
    bool write(std::vector<char> data)
    {
        return true;
    }
    int main()
    {
        std::cout << "call test" << std::endl;
        CDemo ok=test();
        
        CDemo ok2;
        std::cout << "call =" << std::endl;
        ok2=ok;
    
        std::cout << "call test2" << std::endl;
        test2(ok);
    
        std::cout << "call test5" << std::endl;
        test5(std::move(ok2));//因为test5的参数声明为右值了 所以直接传入左值编译不过test5(ok2); 而test2则可以传入左值是调用复制,转入右值时调用移动
     
        std::cout << "call test6" << std::endl;
        ok = test6(std::move(ok));
    
        std::cout << "call test6" << std::endl;
        CDemo&& ok3 = test6(std::move(ok));
        return 0;
    }

    ---------------------------------------------------------------------------

    ---------------------------------------------------------------------------

    ---------------------------------------------------------------------------

    ---------------------------------------------------------------------------

    ---------------------------------------------------------------------------

    ---------------------------------------------------------------------------

    ---------------------------------------------------------------------------

    下面是输出结果

    call test
    移动函数
    call =
    赋值函数
    call test2
    拷贝函数
    call test3
    拷贝函数
    call test4
    移动函数
    call test5
    移动函数
    call test6
    移动函数
    赋值函数
    call test6
    移动函数
  • 相关阅读:
    绪论-1.1.2机器学习
    Django从 URL获取参数的几种方式
    conda创建虚拟换件安装包时报错“无法定位程序输入点OPENSSL_sk_new_reserve于动态链接库C:Users...libssl-1_1-x64.dll”
    执行python manage.py makemigrations出错
    Linux下conda虚拟环境
    云服务器安装mysql
    2020牛客暑期多校训练营(第五场)
    2020牛客暑期多校训练营(第四场)
    2020牛客暑期多校训练营(第三场)
    All with Pairs
  • 原文地址:https://www.cnblogs.com/yuandaozhe/p/15764307.html
Copyright © 2011-2022 走看看