代码之家  ›  专栏  ›  技术社区  ›  Anne Quinn

有没有写move构造函数的简写方法?

  •  0
  • Anne Quinn  · 技术社区  · 2 年前

    我有一个类,它有一个强指针和许多对象成员。为这样的对象编写复制和移动构造函数需要大量乏味的复制/粘贴,然而。。。

    有没有办法缩短这个,而不放弃我美丽的裸指针?比如,如果我可以执行默认生成的移动操作,只需一条额外的指令就可以在之后使裸指针无效?

    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
    };
    
    1 回复  |  直到 2 年前
        1
  •  5
  •   KamilCuk    2 年前

    使用 unique_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
    };