Vectors and polymorphism in C++

No, it won’t.

vector<Instruction> ins;

stores values, not references. This means that no matter how you but that Instruction object in there, it’ll be copied at some point in the future.

Furthermore, since you’re allocating with new, the above code leaks that object. If you want to do this properly, you’ll have to do

vector<Instruction*> ins

Or, better yet:

vector< std::reference_wrapper<Instruction> > ins

I like this this blog post to explain reference_wrapper

This behavior is called object slicing.

Leave a Comment