Possible Duplicates:
How to pass objects to functions in C++?
is there any specific case where pass-by-value is preferred over pass-by-const-reference in C++?
I have members of a class implemented like this:
void aaa(int a, float b, short c)
{
bbb(a, b);
}
void bbb(int a, float b)
{}
If the values of a, b and c were stored in my class as constants, then would it have been better/sensible to use my functions as shown below or as shown above?
void aaa(int& a, float& b, short& c)
void bbb(int& a, float& b)
Does using references give any speed benefits or advantages in this case? Any disadvantages/overheads of references here?
Standard doesn’t have constraints about implementation of references, however usually they’re implemented as autodereferenced pointers (actually with some exceptions). As you probably know, on 32 bit system pointer size is 4 bytes, that means that passing chars, shorts (types with sizeof() less than 4 bytes) by reference maybe considered as somewhat overkilling – using 4 bytes instead of 1 (char) or 2 (short).
In general it depends on whether the rigisters or stack is used for passing parameters: you can save a bit of stack when passing basic types by value, but in case of registers even for chars, 4 bytes will be used, so there’s no point in trying to optimize something with ptrs/refs.