简体   繁体   中英

How do i correctly add shared_pointers to probably derived classes into a std::vector?

I have basically this setup:

class B { /* ... */};
class C1 : public B { /* ... */};
class C2 : public B { /* ... */};

class X
{
  std::vector<shared_ptr<B>> m_vec;
  void addToVector(B* b);
}

addToVector cant know how many classes derive from B and should not care. It will be called like this:

someFunction() {
  C1 tmp;
  /* do something with tmp */
  m_myX.addToVector(&tmp);
}

so at the end of someFunction , tmp is getting out of scope and will be deleted. addToVector has to push_back a shared_ptr to a copy of tmp into the vector, but how can it do that?

void X::addToVector(B* b)
{
  int i = sizeof(b); // allways sizeof(B) :(
  shared_ptr<B> np(b);
  m_vec.push_back(np); // garbage collected after calling fn returns :(
}

What it should do is:

  • make a copy of the object b is pointing to by calling the copy constructor/operator of the correct class
  • push_back a shared_ptr to that copy into the vector.

How can i do this?

You are creating an object on the stack and then giving its address to a shared_ptr which will try to delete an object on the stack, which is undefined behaviour.

The solution is to stop doing that:

void someFunction() {
  C1* c = new C1;
  /* do something with *c */
  m_myX.addToVector(c);
}

Now you have an object on the heap, which can be owned by a shared_ptr . There's no need to make a copy of it.

This will only work correctly if B has a virtual destructor, but that can be avoided (and the code can be made safer and cleaner) by creating a shared_ptr in the first place:

void someFunction() {
  auto c = std::make_shared<C1>();
  /* do something with *c */
  m_myX.addToVector(c);
}

void X::addToVector(std::shared_ptr<B> b)
{
  m_vec.push_back(np);
}

Now the heap object is managed by shared_ptr as soon as it's created, and then safely stored in the vector.

your 'someFunction' looks strange... (why not create tmp as shared_ptr in the first place?)

to make it work you will have to create 'virtual constructor' - add virtual method B* deepCopy() const in B, and implement it in all subclasses, it's body should be based on pattern: { return new DerivedType(*this); } { return new DerivedType(*this); }

If you want to be clean - make deepCopy returning shared_ptr and using make_shared .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM