我有一個簡單的結構,它具有所有定義的構造函數。 它有一個int變量,每個構造函數和賦值運算符都輸出* this的地址,int的當前值和int的一個新值。 移動和複製賦值運算符和構造函數也會打印傳遞值的地址。通過值傳遞的附加移動構造函數
#include <iostream>
struct X
{
int val;
void out(const std::string& s, int nv, const X* from = nullptr)
{
std::cout<<this<<"->"<<s<<": "<<val<<" ("<<nv<<")";
if (from)
std::cout<<", from: ["<<from<<"]";
std::cout<<"\n";
}
X(){out("simple ctor X()",0); val = 0;}
X(int v){out("int ctor X(int)", v);val = v; }
X(const X& x){out("copy ctor X(X&)", x.val, &x);val = x.val; };
X&operator = (const X& x){out("copy X::operator=()", x.val, &x); val = x.val; return *this;}
~X(){out("dtor ~X", 0);}
X&operator = (X&& x){out("move X::operator(&&)", x.val, &x); val = x.val; return *this;}
X(X&& x){out("move ctor X(&&x)", x.val, &x);val = x.val;}
};
X copy(X a){return a;}
int main(int argc, const char * argv[]) {
X loc{4};
X loc2;
std::cout<<"before copy\n";
loc2 = copy(loc);
std::cout<<"copy finish\n";
}
輸出:
0xffdf7278->int ctor X(int): 134523184 (4) 0xffdf727c->simple ctor X(): 134514433 (0) before copy 0xffdf7280->copy ctor X(X&): 1433459488 (4), from: [0xffdf7278] 0xffdf7284->move ctor X(&&x): 1433437824 (4), from: [0xffdf7280] 0xffdf727c->move X::operator(&&): 0 (4), from: [0xffdf7284] 0xffdf7284->dtor ~X: 4 (0) 0xffdf7280->dtor ~X: 4 (0) copy finish 0xffdf727c->dtor ~X: 4 (0) 0xffdf7278->dtor ~X: 4 (0)
什麼是創建一個額外的對象(在這個例子中)地址0xffdf7284的目的是什麼?
那麼,它被稱爲創建適當的價值? –
@ Guy-WithA-gum沒有。由於它不是一個值,所有的方法都有副作用,所以它將它移出,然後調用析構函數。這根本不涉及prvalues。 –
爲什麼它只是不返回「a」,爲什麼它需要從構建值中移走 –