2010-08-05 52 views
1

鉴于类:什么时候会调用ctor?

class C 
{ 
public: 
    C() 
    { 
     cout << "Dflt ctor."; 
    } 
    C(C& obj) 
    { 
     cout << "Copy ctor."; 
    } 
    C(C&& obj) 
    { 
     cout << "Move ctor."; 
    } 
    C& operator=(C& obj) 
    { 
     cout << "operator="; 
     return obj; 
    } 
    C& operator=(C&& obj) 
    { 
     cout << "Move operator="; 
     return obj; 
    } 
}; 

,然后在主:

int main(int argc, char* argv[]) 
{ 
    C c; 
    C d = c; 
    C e; 
    e = c; 
    return 0; 
} 

因为你会从输出看到“常规”拷贝构造函数和operator=的版本被调用,但不是那些与右值ARGS。所以我想问一下在什么情况下可以调用ctor和operator=(C&&)

回答

7

移动构造函数将在右边是临时的时候被调用,或者使用static_cast<C&&>std::move明确地转换为C&&

C c; 
C d(std::move(c)); // move constructor 
C e(static_cast<C&&>(c)); // move constructor 
C f; 
f=std::move(c); // move assignment 
f=static_cast<C&&>(c); // move assignment 
C g((C())); // move construct from temporary (extra parens needed for parsing) 
f=C(); // move assign from temporary 
1

IIRC,您必须使用C d = std::move(c)来使用移动构造函数。

一个例子未经测试,但可能更好地解释使用移动构造函数的:

C&& foo() { C c; return std::move(c); } 
1
std::swap(c,e); // c and e variables declared in your _tmain() 

将调用移动的构造。

1

所有变量都是左值,因此不能隐式移动,因为您可能需要稍后访问它们。另外,复制构造函数和赋值运算符需要const引用。

右值引用适用于右值,即临时对象。为了看到使用的移动构造函数,首先,你将不得不实际创建一个临时对象。另外,不要忘记RVO仍然适用,并且可能会钉住任何或所有std :: cout调用。

你可以使用std :: move(lvalue)从左值创建一个右值。

-1

使用移动运营商的更现实的例子是,如果你有返回对应c & &本地栈上创建这样一个静态类:

static C&& CreateC() 
{ 
    C c(); 
    //do something with your object 
    return c; 
} 

,然后调用它像这样:

C x = CreateC(); // move operator is invoked here 
+0

请在投票时发表评论 – 2016-05-04 20:08:16

相关问题