返回

C++:有效地复制包含 std::vector 的结构

发布时间:2022-07-03 05:35:36 293
# golang
struct MZEntry
{
    uint32_t machineID;
    bool mode;
    uint32_t area;
    uint32_t occupancy;

    
    using ZList = std::vector;
    
    ZList authorisationZ;
    ZList blockExceptionZ;
    
    void clear()
    {
        machineID = 0;
        mode = false;
        area = 0;
        occupancy = 0;
        authorisationZ.clear();
        blockExceptionZ.clear();
    }

    MZEntry(){
        clear();
    }

    MZEntry(const MZEntry& mzEntry)
    {
        machineID = mzEntry.machineID;
        mode = mzEntry.mode;
        area = mzEntry.area;
        occupancy = mzEntry.occupancy;
        authorisationZ = mzEntry.authorisationZ;
        blockExceptionZ = mzEntry.blockExceptionZ;
    }
};

我的代码中有上述结构。我明确声明复制构造函数有什么好处吗?

假设我有两个变量

MZEntry entry_1;
MZEntry entry_2;

如果我这样做了entry_1 = entry_2,将entry_2复制到entry_1包括向量?

或者是否需要明确的副本构造函数。此外,上述显式复制构造函数是否更有效?

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像