我有一个类,它有一个强指针和许多对象成员。为这样的对象编写复制和移动构造函数需要大量乏味的复制/粘贴,然而。。。
有没有办法缩短这个,而不放弃我美丽的裸指针?比如,如果我可以执行默认生成的移动操作,只需一条额外的指令就可以在之后使裸指针无效?
class Example {
public:
Example()
: m_multiplexDevice(getDevice(B737M_mpDevice, true))
{
engageDevice(m_multiplexDevice);
}
Example(Example && other)
: m_multiplexDevice (other.m_multiplexDevice )
, m_pilotStack (std::move(other.m_pilotStack ))
, m_pasStack (std::move(other.m_pasStack ))
, m_pitchAttackLimits (std::move(other.m_pitchAttackLimits))
, m_yawToPitchBalance (std::move(other.m_yawToPitchBalance))
, m_engineBalance (std::move(other.m_engineBalance ))
{
other.m_multiplexDevice = NULL;
}
Example & operator=(Example && other) {
if (this != &other) {
// ignore that this is incorrect (not properly destroying in assignment),
// working with client code that kinda sucks
m_multiplexDevice = other.m_multiplexDevice;
m_pilotStack = std::move(other.m_pilotStack );
m_pasStack = std::move(other.m_pasStack );
m_pitchAttackLimits = std::move(other.m_yawToPitchBalance );
m_yawToPitchBalance = std::move(other.m_yawToPitchBalance );
m_engineBalance = std::move(other.m_engineBalance );
m_multiplexDevice = NULL;
}
return *this;
}
Example(const Example & other) =delete;
Example & operator=(const Example & other) =delete;
~Example() {
if (m_multiplexDevice)
disengageDevice(m_multiplexDevice);
delete m_multiplexDevice;
}
private:
char STRONG * m_multiplexDevice;
std::vector<uint32> m_pilotStack;
std::vector<uint32> m_pasStack;
std::vector<uint32> m_pitchAttackLimits;
std::vector<uint32> m_yawToPitchBalance;
std::vector<uint32> m_engineBalance;
// ... etc
};