对象并集-不编译

时间:2019-04-17 13:51:31

标签: c++

我正在尝试使此代码正常工作,但是添加Apartment实例的匿名联合会导致问题。

template <typename T, typename D>
struct Block {

};

template <typename B, typename T>
struct Building {

};

struct TestBlock : public Block<TestBlock, uint32_t> {

    static constexpr uint32_t base = 0;


    struct Apartment : Building<Apartment, TestBlock> {

    public:
        using Building<Apartment, TestBlock>::operator=;

        Apartment(Apartment const & b) : contam_levels(b.contam_levels) {}
        Apartment(uint32_t const c = 0) : contam_levels(c) {}

    private:
        union {
            struct
            {
                uint32_t floor1 : 3;
                uint32_t : 4;
                uint32_t floor2 : 7;
                uint32_t : 3;
                uint32_t floor3 : 7;
                uint32_t : 8;
            };
            uint32_t contam_levels;
        };
    };

    union
    {
        Apartment volatile BayviewMaple;
        Apartment volatile HomeBuilding;
    };
};

TestBlock testBlock;

我收到错误C2280:TestBlock :: TestBlock(void)试图引用已删除的函数。

删除匿名联合允许程序进行编译。如果保留联合,但从类定义中删除2个Apartment构造函数,则程序将编译。当我同时保留构造函数和匿名联合时,为什么程序会失败?

是否有办法“保留”联合和构造函数这两个功能?

0 个答案:

没有答案