I have always been taught that non-primitive types should be passed by const reference rather than by value where possible, ie:
void foo(std::string str);//bad
void foo(const std::string &str);//good
But I was thinking today that maybe actually some simple user defined types may actually be better passed by value eg:
class Vector2
{
public:
float x, y;
...constructors, operators overloads, utility methods, etc...
};
void foo(Vector2 pos);
void foo(const Vector2 &pos);//is this really better than by value?
void foo(float x, float y);//after all isn't by value effectively doing this?
My thought is that by passing the Vector2 by reference, it is actually more expensive than passing by value since the compiler is now using a pointer and dereferencing to access the const Vector2 &pos version?
Is this the case? Are simple objects best off passed by value? Where should the line be drawn?