9

The title pretty much says it all. Basically, is it legal to do this:

class Base {
    //stuff
}

class Derived: public Base {
    //more stuff
}

vector<Base> foo;
Derived bar;
foo.push_back(bar);

Based on other posts I've seen, the following is okay, but I don't want to use pointers in this case because it's harder to make it thread safe.

vector<Base*> foo;
Derived* bar = new Derived;
foo.push_back(bar);
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
Beezum
  • 341
  • 1
  • 4
  • 12
  • Why is thread-safety in any way relevant here? Did you mean exception-safety perhaps? If so, yes, `push_back` could throw an exception, leaving out a dangling pointer. You can use smart pointers to handle that. – DanielKO Aug 09 '12 at 19:38
  • @DanielKO I'm using an STL container as a buffer between a producer and consumer thread. I don't want to fill the container with pointers because even though I've protected the container itself with a mutex, once any thread has a pointer to some other data, that data is not protected. – Beezum Aug 09 '12 at 21:12
  • That itself is not a thread-safety problem. Even without pointers one can also mess around with the elements in a thread-unsafe way. All you have to do is to define the proper semantics so the contents (stored as pointers or directly) are not modified without the proper concurrency consistency mechanisms. In particular, if you are afraid a user of the class might store a pointer to the element without "permission", what makes you think he won't take a pointer or a reference in the same way? – DanielKO Aug 09 '12 at 21:29

3 Answers3

16

No, the Derived objects will be sliced: all additional members will be discarded.

Instead of raw pointers, use std::vector<std::unique_ptr<Base> >.

Community
  • 1
  • 1
ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • @LuchianGrigore "can vector contain objects of type Derived?" -> No, they will be sliced, so they'll be just `Base` objects. It isn't illegal, but it's not true either. – mfontanini Aug 09 '12 at 17:59
  • 1
    @LuchianGrigore "No, vector can't contain objects of type Derived". Because once they're sliced they aren't `Derived` any more. – ecatmur Aug 09 '12 at 17:59
  • 1
    @Heisenbug the `Base` that ends up in the vector is copy constructed from the `Derived` object being pushed. If a copy construction generates a leak, then you've got a problem in your code :D. – mfontanini Aug 09 '12 at 18:02
  • 1
    @Heisenbug - No, there is no leak. The vector stores a copy of the object. Being a `vector`, it just copies the Base part. – Bo Persson Aug 09 '12 at 18:03
  • @ecatmur Can you explain how smart pointers will serve the purpose? Won't object slicing still happen? – CinCout Mar 25 '14 at 13:18
  • @gargankit the templated converting constructor of `unique_ptr` transfers ownership of the contained pointer, so the `Derived` object is not sliced. – ecatmur Mar 25 '14 at 13:53
4

It's legal but suffers from object slicing. Basically, you'll have a vector of Base objects. No polymorphism, type info will be lost for derived objects... It's as if you'd just be adding Base objects to the vector.

You can use smart pointers instead.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1
vector<Base> foo;
Derived bar;
foo.push_back(bar);

This is equal to pushing Base object, because push_back is declared like this:

void push_back ( const T& x );

So, compiler will do implicit downgrading conversion and do copy into vector memory pool. No, it is not possible to contain Derived inside vector<Base>. They will be Base.

If you add some virtual function to Base then override it in Derived, create Derived object, push it into vector<Base> and then call it from vector's new object, you will see that Base implementation is called

Roman Saveljev
  • 2,544
  • 1
  • 21
  • 20