理解move

#include <initializer_list>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct B {
    int v = 100;
    ~B() { v = 0; }
};

struct A {
    A(B* p) { aa = p; }
    B* aa = nullptr;
    A(A&& obj) { aa = obj.aa; }
    A(const A& obj) { aa = obj.aa; }
};
void f(vector<A>& a) {
    deque<A> q;
    B b;
    A obj(&b);
    q.push_back(obj);
    a.push_back(std::move(q.front()));
    q.pop_front();
}
int main() {
    std::vector<A> a;
    f(a);
    cout << a.begin()->aa->v << endl;
}
posted @ 2023-08-28 16:24  ijpq  阅读(8)  评论(0编辑  收藏  举报