本文介绍了a =默认移动构造函数等于成员式移动构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是

  struct示例{
int a,b;
示例(int mA,int mB):a {mA},b {mB} {}
示例(const Example& mE):a {mE.a},b {mE.b} }
示例(示例&&&mE):a {move(mE.a)},b {move(mE.b)} {}
示例& operator =(const Example& mE){a = mE.a; b = mE.b; return * this; }
示例& operator =(Example&& mE){a = move(mE.a); b = move(mE.b); return * this; }
}

相当于

  struct示例{
int a,b;
示例(int mA,int mB):a {mA},b {mB} {}
示例(const Example& mE)
示例(示例&& mE)= default;
示例& operator =(const Example& mE)= default;
示例& operator =(Example&& mE)= default;
}




$ b $

  struct示例{
int a,b;
示例(int mA,int mB):a {mA},b {mB} {}
示例(const Example& mE)
示例(示例&& mE)= default;
示例& operator =(const Example& mE)= default;
示例& operator =(Example&& mE)= default;
}

此版本将允许您跳过正文定义。

$但是,当你声明显式默认函数时,你必须遵循一些规则:


Is this

struct Example {
    int a, b;
    Example(int mA, int mB) : a{mA}, b{mB}               { }
    Example(const Example& mE) : a{mE.a}, b{mE.b}        { }
    Example(Example&& mE) : a{move(mE.a)}, b{move(mE.b)} { }
    Example& operator=(const Example& mE) { a = mE.a; b = mE.b; return *this; }
    Example& operator=(Example&& mE)      { a = move(mE.a); b = move(mE.b); return *this; }
}

equivalent to this

struct Example {
    int a, b;
    Example(int mA, int mB) : a{mA}, b{mB} { }
    Example(const Example& mE)            = default;
    Example(Example&& mE)                 = default;
    Example& operator=(const Example& mE) = default;
    Example& operator=(Example&& mE)      = default;
}

?

解决方案

Yes both are the same.

But

struct Example {
    int a, b;
    Example(int mA, int mB) : a{mA}, b{mB} { }
    Example(const Example& mE)            = default;
    Example(Example&& mE)                 = default;
    Example& operator=(const Example& mE) = default;
    Example& operator=(Example&& mE)      = default;
}

This version will permits you to skip the body definition.

However, you have to follow some rules when you declare explicitly-defaulted-functions :

这篇关于a =默认移动构造函数等于成员式移动构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 12:09