有时您仍然想要默认的移动构造函数行为(按成员移动),但也想要修改移动的对象。以下面的场景为例
class Base{...}; // some move-constructible class
class Member{...}; // also move constructible
class Derived : public Base
{
public:
// does member-wise move, but nothing more
// Derived(Derived &&) = default;
// does member-wise move, same as default
// and also modifies `other`
Derived(Derived && other)
: a(std::move(other.a))
, b(std::move(other.b))
, ... // the rest of members
{
other.put_into_invalid_state_or_something();
}
//...............................................
// More functions
//...............................................
private:
// a lot of members...
Member a,b,c,d,e,f,g,h,i;
};
这可能是一个愚蠢的问题,但有没有办法避免为每个成员编写移动?