2015-04-01 116 views
0

我正在阅读C++ Primer,这段代码让我有点困惑。也许我已经阅读过,但忘记了它的内容。拷贝构造函数的区别?

这段代码有2个拷贝构造函数,但我不知道有什么区别他们

class Quote { 
public: 
    Quote() = default; 
    Quote(const Quote&) = default; // <<== this one 
    Quote(Quote&&) = default;   // <<== and this one 
    Quote& operator=(const Quote&) = default; 
    Quote& operator=(Quote&&) = default; 
    virtual ~Quote() = default; 
} 

就是一般的差之间?

和双“&”是什么意思?

+1

请参阅[本](http://stackoverflow.com/a/4549167/2899559)回答。 – 2015-04-01 07:14:53

+0

复制构造函数和移动构造函数。 – Jarod42 2015-04-01 07:14:58

+0

thnx!我只记得.. RValue参考是用于移动即将销毁的物品 – CantThinkOfAnything 2015-04-01 07:24:37

回答

4

它们不是都是复制构造函数,只有第一个:Quote(const Quote&) = default;。第二个是移动构造函数,在移动语义和C++ 11上做一些阅读。

+1

''Quote(const Quote&)= default;''是复制构造函数。 ''Quote&operator =(const Quote&)= default''是赋值操作符。 'Quote(Quote &&)= default''是移动构造函数。 ''Quote&operator =(Quote &&)= default''是移动操作符。 – 2015-04-01 07:17:33

+0

嗯,我明白了...虽然在代码片中说“//成员复制”所以多数民众赞成在混淆我。但我现在知道了,thnx – CantThinkOfAnything 2015-04-01 07:25:43