I don’t understand something about push_back.
When I use push_back on an object , does it copy all its elements using the object’s
copy constructor? (what if the object doesn’t have copy constructor?)
another question:
vector<Course*> cs;
Course* c = new CScourse(); //CScourse inherits Course
cs.push_back(c);
if I use
delete c;
would it delete what I just pushed into the vector?
Yes every objects are copied in a
vector. If thevectorhas to grow, each element will be copied to the new location. In your case, you are storing pointers toCourseso only the pointers get copied, the actual objects remain the same.If you
delete c;, it will indeed delete the object pointed to bycthat you pushed in yourvector. Thevectorwill still contain a pointer to the object that wascthough, this is considered a dangling pointer. You would usually remove it from yourvectorbefore deleting it.