在一个玩具类的移动构造函数的实现过程中,我注意到了一个模式:
array2D(array2D&& that)
{
data_ = that.data_;
that.data_ = 0;
height_ = that.height_;
that.height_ = 0;
width_ = that.width_;
that.width_ = 0;
size_ = that.size_;
that.size_ = 0;
}
模式显然是:
member = that.member;
that.member = 0;
所以我写了一个预处理器宏来让窃取变得不那么冗长和容易出错:
#define STEAL(member) member = that.member; that.member = 0;
现在实现如下所示:
array2D(array2D&& that)
{
STEAL(data_);
STEAL(height_);
STEAL(width_);
STEAL(size_);
}
这有什么缺点吗?是否有不需要预处理器的更清洁的解决方案?