std::vector vs dynamic array passing by value

Viewed 112

Hello I am comming from c to c++ and I've been wondering why can std::vector be passed by value.

I assume passing dynamicaly allocated array by value is not possible as that would only copy the pointer.

How is it then possible for a vector to be coppied, if inside of a vector class is same pointer. It has to somehow know how to reconstruct it into another object.

1 Answers

std::vector knows how many elements are stored in the dynamic memory. It is a simple matter to allocate a new buffer of that size and copy the contents into that new memory. All of this happens in the copy constructor.

Related