是否有编写移动构造函数的简写方法?

Ann*_*inn 0 c++ constructor move

我有一个类,有一个强指针和很多对象成员。为这样的对象编写复制和移动构造函数涉及大量繁琐的复制/粘贴,但是......

有什么办法可以缩短这个时间,而又不放弃我美丽的裸指针吗?就像我是否可以执行默认生成的移动操作,然后只需要一条额外的指令来使裸指针无效?

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
};
Run Code Online (Sandbox Code Playgroud)

Kam*_*Cuk 5

将 aunique_ptr与自定义删除器一起使用。

#include <memory>
#include <vector>
#include <cstdint>
#define STRONG /* ??? */
void disengageDevice(char STRONG*);
#define B737M_mpDevice 1
char STRONG *getDevice(int, ...);
void engageDevice(char STRONG *);


class Example {
public:
    Example() 
        : m_multiplexDevice(
            getDevice(B737M_mpDevice, true),
            deconstruct_m_multiplexDevice)
    {
        engageDevice(m_multiplexDevice.get());
    }

    static void deconstruct_m_multiplexDevice(char STRONG *p) {
        if (p) {
            disengageDevice(p);
            delete p;
        }
    }

private:
    std::unique_ptr<
        char STRONG,
        decltype(&deconstruct_m_multiplexDevice)
    > m_multiplexDevice;
    std::vector<uint32_t> m_pilotStack;
    std::vector<uint32_t> m_pasStack;
    std::vector<uint32_t> m_pitchAttackLimits;
    std::vector<uint32_t> m_yawToPitchBalance;
    std::vector<uint32_t> m_engineBalance;
    // ... etc
};
Run Code Online (Sandbox Code Playgroud)