I have a class Outer which contains an Inner member and owns a vector of unique_ptr elements:
using Elements = std::vector<std::unique_ptr<Element>>;
class Outer
{
void call()
{
_inner.aMethod(_vec);
}
Inner _inner;
Elements _vec; // This should remain the owner of each Element
};
Inner receives the vector of unique_ptr elements and it transfers ownership to it's own vector class member:
class Inner
{
public:
Inner() = default;
~Inner() = default;
void aMethod(Elements& vec)
{
_vec = std::move(vec);
}
private:
Elements _vec; // This is a vector of unique_ptr but I don't want this class to own the memory
};
I stupidly used std::move() because otherwise the compiler complained I was trying to call a deleted function (probably the copy constructor) on each vector element.
I have an illegal memory access and I believe it is because both classes think they own the vector elements and one has tried to delete an already-deleted Element.
How do I have Outer owning the memory and just pass the elements to Inner to use (not take ownership)?
Aucun commentaire:
Enregistrer un commentaire