unique_ptr의 벡터에 다른 파생 객체를 추가하는 방법은 무엇입니까? 포인터의 벡터와 항목을 다음 기본 포인터를 개체를 만들고 추가 할 수있는 포인터를 전달하지만 make_unique dosn't 포인터를 가져올 및 파생 된 objs 각 유형에 대한 오버로드 된 함수를 만들고 싶지 않아.unique_ptr의 벡터에 파생 된 객체를 추가하는 방법
가 질문을 후속 ----------------------------------
class Base{...};
class Item:public Base{...};
Add_Item(Base* thing)
{
vec.push_back(thing);
}
//Problem with this is i have multiple derived objs and would rather have 1 Add_Item instead of multiple Add_Item functions.(if possible)
Add_Item(Item thing)
{ //this runs,but i lose anything that was in Item class
vec.push_back(make_unique<Base>(thing));
}
/당신은 unique_ptr의 벡터를 다른 벡터 안에 가질 수 있습니까? 나는이 방법을 시도했다. (두 가지 방법으로 템플릿 Add_Item 함수와 Item Add_Item 함수로 나를 보여줬다.) 그러나 둘 다 오류를 생성했습니다.
class Item: public Item
{ vector<unique_ptr<Base>>inv }
Item bag;
Item n_bag;
bag.Add_Item<I>(n_bag);
최대, 아니 당신은 캔트의
std::make_unique()
도우미 함수는 지정된 형태의 새로운 객체를 생성
도움을 주셔서 감사합니다. 다음 질문을 따르십시오. unique_ptr의 벡터를 unique_ptr의 다른 벡터 안에 포함시킬 수 있습니까? – user3085497