With the default copy constructor, any copy will be shallow and will not make a copy of the memory pointed to by data
. So when you pass by reference, only the two integers and pointer will be passed, for a total of approximately 12 bytes (depending on your architecture, i.e. pointer size).
That difference is so small, that it does not really matter whether you pass it by value or by reference. The latter may be slightly faster because the pointer can probably always be passed in via a CPU register and the 12 bytes may not, but that is really micro-optimization.
Personally I pass anything but primitive types by (const
) reference, by default, unless I have a reason not to (see jupp0r's answer). Major advantage during development is that as the class grows I don't have to worry about when it becomes too big and change all my functions to pass by reference.
As far as the default C++ iterators go: note that they are meant to basically be just pointers. In fact I know that for Microsoft Visual C++ compiling in Release mode with optimizations enabled, iterators for contiguous data structures such as std::vector
will reduce to just that. I am not aware if other compilers do this as well, I would think so. However you will notice that if you disable optimizations, then suddenly there is a difference between writing it++
and ++it
in loop increments, for example - purely because of the additional copy operation in the former case. So even "cheap to copy" may have an impact if you do it often enough. As usual: when worried about performance, measure and decide based on the numbers.