Been learning C++ for a while now but I'm still confused as to when I should pass-by-value and when I should use pass-by-reference. Googling this problem yields the following information:
- Pass-by-value creates a copy of the variable which takes up extra memory and has a processing overhead. HOWEVER, compiler may optimise to pass-by-reference where appropriate but you shouldn't count on it.
So it seems clear that pass-by-reference, or better yet, const reference is the way to go in most cases but...
- Some sources say passing-by-reference still has the overhead of constructing and passing a pointer like value which points to the data being referenced, so for types smaller than / same size as* a pointer pass-by-value is quicker.
- Other sources say that the above is in fact wrong and that most compilers have special optimisations for passing integral types and only those should be pass-by-value and everything else should be pass-by-reference.
*( sources vary on which is faster/better in the case of the same size )
I realise which you should use is also dependent on the scenario in which it is used. So here is a context:
ADTClass myADT;MyType myVar = <some valid initialisation>;if( MyFunction() ){ myADT.m_val = myVar;}else{ myADT.m_otherVal = myVar;}
K, I'm happy here since myVar gets copied directly to the target location ( I think ). Now:
ADTClass myADT;
MyType myVar = <some valid initialisation>;
myADT.SetOneOfMyVars( myVar );
Where SetOneOfMyVars like this:
void ADTClass::SetOneOfMyVars( MyType myVar ) { if( MyFunction() ) { m_val = myVar; } else { m_otherVal = myVar; }}
Or like this:
void ADTClass::SetOneOfMyVars( const MyType& myVar ) { if( MyFunction() ) { m_val = myVar; } else { m_otherVal = myVar; }}
And MyType is any type from an int to an 128 byte class.
Which one is faster? Does it depend on the compiler? The size of MyType? Whether MyType is an integral type? How does it get affected if SetOneOfMyVars is inlined / cannot be inlined?
Getting very confused about exactly what happens behind the scenes when you pass something to a function!