小编Ego*_*lev的帖子

如何延长局部变量的生命周期或使用引用的正确方法

我正在开发一些课程,并提出了这个问题.考虑我有以下课程:

struct A
{
    int *p;
    A() 
    {
        p = new int(1); 
        cout << "ctor A" << endl; 
    }
    A(const A& o) 
    { 
        cout << "copy A" << endl;
        p = new int(*(o.p));
    }
    A(A&& o) 
    {
        cout << "move A" << endl;
        p = std::move(o.p);
        o.p = NULL;
    }
    A& operator=(const A& other)
    {       
        if (p != NULL)
        {
            delete p;
        }
        p = new int(*other.p);
        cout << "copy= A" << endl;
        return *this;
    }
    A& operator=(A&& other)
    { …
Run Code Online (Sandbox Code Playgroud)

c++ reference move object-lifetime c++11

8
推荐指数
1
解决办法
770
查看次数

标签 统计

c++ ×1

c++11 ×1

move ×1

object-lifetime ×1

reference ×1