C++ C++;17复制构造函数,在std::无序映射上深度复制

C++ C++;17复制构造函数,在std::无序映射上深度复制,c++,recursion,c++17,deep-copy,unordered-map,C++,Recursion,C++17,Deep Copy,Unordered Map,我在实现复制构造函数时遇到问题,该构造函数是在我的预制子参与者上执行深度复制所必需的,它是 std::unordered_map<unsigned, PrefabActor *> child_actor_container; 以下是我的实施: class DataFileInfo { public: DataFileInfo(std::string path, std::string filename ); DataFileInfo(const DataFileIn

我在实现复制构造函数时遇到问题,该构造函数是在我的预制子参与者上执行深度复制所必需的,它是

std::unordered_map<unsigned, PrefabActor *> child_actor_container;
以下是我的实施:

class DataFileInfo
{
public:
    DataFileInfo(std::string path, std::string filename );
    DataFileInfo(const DataFileInfo & rhs);
    virtual ~DataFileInfo();
    // all other functions implemented here
private:
    std::unordered_map<std::string, std::string> resource_info;
    bool selection;
};

class PrefabActor : public DataFileInfo
{
public:

    PrefabActor(std::string path, std::string filename , std::string object_type, PrefabActor * parent_actor = nullptr);
    PrefabActor(const PrefabActor & rhs);

    ~PrefabActor();

    // all other function like add component, add child actor function are here and work fine 

private:
    unsigned child_prefab_actor_key; // the id key
    PrefabActor* parent_prefab_actor; // pointer to the parent actor

    std::unordered_map<ComponentType, Component*> m_ObjComponents; // contains a map of components like mesh, sprite, transform, collision, stats, etc.

    //I need to be able to deep copy this unordered map container and be able to recursive deep copy 
    std::unordered_map<unsigned, PrefabActor *> child_actor_container; // contains all the child actors

    std::unordered_map<std::string, std::string> prefab_actor_tagging; // contains all the tagging

};
类数据文件信息
{
公众:
DataFileInfo(std::string路径,std::string文件名);
DataFileInfo(常量DataFileInfo和rhs);
虚拟~DataFileInfo();
//此处实现的所有其他功能
私人:
std::无序的地图资源信息;
布尔选择;
};
类:公共数据文件信息
{
公众:
Prefactor(std::string path,std::string filename,std::string object_type,prefactor*parent_actor=nullptr);
预制工(施工预制工和rhs);
~z~演员();
//所有其他函数,如addcomponent、addchild-actor函数,都在这里,可以正常工作
私人:
未签名的子项\u预置\u参与者\u键;//id键
prefactor*parent\u prefact\u actor;//指向父参与者的指针
std::无序映射m_ObjComponents;//包含网格、精灵、变换、碰撞、统计等组件的映射。
//我需要能够深度复制这个无序的映射容器,并能够递归深度复制
std::无序映射子参与者容器;//包含所有子参与者
std::无序的\u映射预制\u参与者\u标记;//包含所有标记
};

您必须手动复制条目:

PrefabActor(const PrefabActor & rhs)
{
    for(const auto& entry:  rhs.child_actor_container)
    {
        child_actor_container[entry.first] = new PrefabActor(*entry.second);
    }
}
当然,您还需要更改子对象的父对象


您还应该指出谁拥有
prefactor
对象。此处可能存在内存泄漏。

如果需要深度复制,为什么要使用指针?递归不是问题,不管你指的是哪一个指针,你都需要实现prefactor的copy-ctor?你是说std::unordered_map作为预制体*?我的意思是,为什么不
std::unordered_map
?for(auto-it=rhs.child_-actor_-container.begin();it!=rhs.child_-actor_-container.end();+it,+child_-key){prefactor*child_=new prefactor(*(it->second));child_-prefactor->>setparenter(child_键,child_预置);}这可以吗?什么是
child\u key
?我以为你想要一个深度克隆。如果你有额外的ID,那么是的,你必须调整此代码以适应你没有提出的问题。child\u key只是一个未签名的值,它是child\u prepare\u actor\u key哦,好的。正如我所说的,你需要调整代码以适应跟踪和识别你的具体方式你的目标。
PrefabActor(const PrefabActor & rhs)
{
    for(const auto& entry:  rhs.child_actor_container)
    {
        child_actor_container[entry.first] = new PrefabActor(*entry.second);
    }
}