其他分享
首页 > 其他分享> > std::move使用实例

std::move使用实例

作者:互联网

std::move使用实例

#include <iostream> 
#include <memory>

class TestPtr : public std::enable_shared_from_this<TestPtr>
{
public:
    TestPtr(std::string  str = "000") {std::cout << "TestPtr construct:" <<this << std::endl; strName = str;}
    ~TestPtr(){std::cout << "TestPtr desconstruct:"<< strName <<" this:" << this << std::endl;}
    
    std::shared_ptr<TestPtr> get(TestPtr * ptr)
    {
        std::shared_ptr<TestPtr> curPtr(ptr);
        std::cout << "    inner curPtr:" << curPtr << std::endl;
        std::cout << "    inner ptr:" << ptr << std::endl;
        std::cout << "    inner ptr strName:" << ptr->strName << std::endl;
        std::cout << "    inner curPtr use_cout:" << curPtr.use_count() << std::endl;
        return curPtr;
    }

    std::shared_ptr<TestPtr> getPtr ()
    {
        return shared_from_this();
    }

    //移动构造函数
    TestPtr(TestPtr && test):strName(std::move(test.strName))
    {
        std::cout << "move operator () " << std::endl;
    }
    
    //移动赋值函数
    TestPtr& operator = (TestPtr && test)
    {
        std::cout << "move operator = " << std::endl;
        strName = std::move(test.strName);
    }
    void print()
    {
        std::cout << "print this:" << this << std::endl;
    }

    void getstr() {

        std::cout << "str:" << strName <<std::endl;
    }
public:
    std::string strName;
};


int main()
{
    std::shared_ptr<TestPtr> testPtr;
    std::cout << "---------------------ptr-----------------------------\r\n" << std::endl;

    {
        TestPtr * ptr = new TestPtr(std::string("123"));
        std::cout << "ptr:" << ptr << std::endl;
        testPtr = testPtr->get(std::move(ptr));
        std::cout << "move end, ptr:" << ptr << "  strName:" << ptr->strName << std::endl;
        std::cout << "testPtr use_count:" << testPtr.use_count() << std::endl;

    }
    std::cout << "\r\n-----------------------move-prt---------------------------\r\n" << std::endl;
    
    //std::move() 
    testPtr->print();
    std::shared_ptr<TestPtr> testPtr1;
    testPtr1 = std::move(testPtr);
    testPtr->print();
    testPtr1->print();
    std::cout << "move use_cout:" << testPtr1.use_count() << std::endl;
    std::cout << "\r\n-----------------------shared__from_this----------------------------\r\n" << std::endl;

    //shared_from_this()
    std::shared_ptr<TestPtr> testPtr3;
    testPtr3 = testPtr1->getPtr();
    std::cout << "shared_from_this use_cout:" << testPtr1.use_count() << "    test3:" << testPtr3 << std::endl;

    //赋值
    std::shared_ptr<TestPtr> testPtr4;
    testPtr4 = testPtr3;
    std::cout << "=                use_cout:" << testPtr1.use_count() << "    test4:" << testPtr4<< std::endl;


    std::cout << "\r\n------------------------move-struct------------------------------\r\n" << std::endl;
    TestPtr stru1(std::string("223"));
    std::cout << "    stru1:" << &stru1 << "  strName :" << stru1.strName  << std::endl;

    auto stru2  = std::move(stru1);
    std::cout << "    stru1:" << &stru1 << "  strName :" << stru1.strName  << std::endl;
    std::cout << "    stru2:" << &stru2 << "  strName :" << stru2.strName  << std::endl;

    TestPtr stru3;
    std::cout << "    stru3:" << &stru3 << "  strName :" << stru3.strName  << std::endl;
    stru3  = std::move(stru2);
    std::cout << "    stru2:" << &stru2 << "  strName :" << stru2.strName  << std::endl;
    std::cout << "    stru3:" << &stru3 << "  strName :" << stru3.strName  << std::endl;

    stru1.print();
    
    std::cout << "\r\n------------------------------------------------------\r\n" << std::endl;

    return 0;

}

执行结果:

---------------------ptr-----------------------------

TestPtr construct:0x2295040
ptr:0x2295040
    inner curPtr:0x2295040
    inner ptr:0x2295040
    inner ptr strName:123
    inner curPtr use_cout:1
move end, ptr:0x2295040  strName:123
testPtr use_count:1

-----------------------move-prt---------------------------

print this:0x2295040
print this:0
print this:0x2295040
move use_cout:1

-----------------------shared__from_this----------------------------

shared_from_this use_cout:2    test3:0x2295040
=                use_cout:3    test4:0x2295040

------------------------move-struct------------------------------

TestPtr construct:0x7fffaf782990
    stru1:0x7fffaf782990  strName :223
move operator ()
    stru1:0x7fffaf782990  strName :
    stru2:0x7fffaf7829b0  strName :223
TestPtr construct:0x7fffaf7829d0
    stru3:0x7fffaf7829d0  strName :000
move operator =
    stru2:0x7fffaf7829b0  strName :000
    stru3:0x7fffaf7829d0  strName :223
print this:0x7fffaf782990

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

TestPtr desconstruct:223 this:0x7fffaf7829d0
TestPtr desconstruct:000 this:0x7fffaf7829b0
TestPtr desconstruct: this:0x7fffaf782990
TestPtr desconstruct:123 this:0x2295040

总结:使用std::move的时候定义的类或者结构体要支持移动构造函数或者移动赋值函数,只有在支持的情况下,std::move才能起到减少拷贝的作用

标签:std,cout,move,TestPtr,实例,ptr,strName
来源: https://blog.csdn.net/F_Beiling/article/details/122306286