【发布时间】:2018-11-04 12:30:56
【问题描述】:
我在实现对我的预制子actor执行深度复制所需的复制构造函数时遇到问题,即
std::unordered_map<unsigned, PrefabActor *> child_actor_container;
它也需要能够递归,因为里面的PrefabActor *可能还有另一层子actor容器。
类似这样的:
layer
1st | 2nd | 3rd
Enemy
Enemy_Body
Enemy_Head
Enemy_hand and etc
Enemy_Weapon
这是我的实现:
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
};
【问题讨论】:
-
如果你想要深拷贝,为什么要使用指针?递归不是问题,无论如何都需要实现 PrefabActor 的 copy ctor。
-
你指的是哪个指针?你的意思是 std::unordered_map
as PrefabActor * ? -
我的意思是,为什么不
std::unordered_map<unsigned, PrefabActor>?
标签: c++ recursion c++17 deep-copy unordered-map