Reputation: 5826
Say I have the following pass by reference:
foo(std::vector<someClass *> const &bar)
compares to pass by value
foo(std::vector<someClass *> bar)
I read that when it comes to primitive datatypes, using pass by value would be prefered whereas pass by reference for large data like class or struct. So what about vector of pointers to objects?
Upvotes: 1
Views: 726
Reputation: 6395
It's simple about the amount of bytes that need to get copied.
When you pass an int
, there is not much difference (if any) between its size and its pointer's size. Because the value is typically already in a register, it is slightly faster.
If you pass a larger structure or array, copying the content (for by-value) takes longer than copying its pointer (for by-ref).
vector of pointers to objects again could be many bytes, so reference is faster.
Upvotes: 4