隐式移动构造函数
Implicit move constructor
隐式移动构造函数到底在做什么?例如,对于以下 class,隐式移动构造函数会是什么样子(您能否提供此隐式构造函数的一些示例实现):
struct A
{
A() = default;
A(A && other) = default;
int a;
};
struct B : public A
{
int b;
int * c;
};
实现会像这样吗:
B(B && other) : A(std::move(other)), b(std::move(other.b)), c(std::move(other.c)) {}
来自cppreference.com:
For union types, the implicitly-defined move constructor copies the
object representation (as by std::memmove). For non-union class types
(class and struct), the move constructor performs full member-wise
move of the object's bases and non-static members, in their
initialization order, using direct initialization with an xvalue
argument. If this satisfies the requirements of a constexpr
constructor, the generated move constructor is constexpr.
基础 class 构造函数在派生构造函数之前运行。
隐式移动构造函数到底在做什么?例如,对于以下 class,隐式移动构造函数会是什么样子(您能否提供此隐式构造函数的一些示例实现):
struct A
{
A() = default;
A(A && other) = default;
int a;
};
struct B : public A
{
int b;
int * c;
};
实现会像这样吗:
B(B && other) : A(std::move(other)), b(std::move(other.b)), c(std::move(other.c)) {}
来自cppreference.com:
For union types, the implicitly-defined move constructor copies the object representation (as by std::memmove). For non-union class types (class and struct), the move constructor performs full member-wise move of the object's bases and non-static members, in their initialization order, using direct initialization with an xvalue argument. If this satisfies the requirements of a constexpr constructor, the generated move constructor is constexpr.
基础 class 构造函数在派生构造函数之前运行。