I know you pass by reference to a function in C++ when you want to change the value of the original variable. But you can also pass by reference when you want the program to be more efficient and if you don't want to change anything in the variable passed to the function, you just make it const. My question is, why not always have your functions accept variables passed by const reference if it is more efficient than just passing the variable and having the compiler create a new one within the scope of the function? To extend the question, what would be a case where a function WOULD need to copy a variable over passed through the parameters?
However passing const reference is more correct - you avoid possibility of null pointer and don't let modify original object. Mostly identical. However const reference can help certain optimizations in some cases (inlining functions, for example). Constant reference is more readable, so that's why I recommend it.
The correct statement is "C does not support implicitly passing a variable by reference" -- you need to explicitly create a reference (with & ) before calling the function and explicitly dereference it (with * ) in the function.
C does not support references or passing by reference. You should use pointers instead and pass by address. Pass-by-value is efficient for primitive types, but does a shallow copy for structs. In C++ it makes a LOT of sense to pass objects by reference for efficiency.
Passing by const reference is the preferred way to pass around objects as a smart alternative to pass-by-value.
When a reference or pointer (including const reference) is passed around in C++ (or C), the programmer is assured that no special code (user-defined or compiler-generated functions) will be executed, other than the propagation of the address value (reference or pointer). This is a clarity of behavior that C++ programmers find comfortable with.
However, C++ makes an exception and lets you pass rvalues into functions that take their argument by const reference, because, intuitively, you shouldn't be able to modify an object through a const reference.
When passing by value, the called function can't store into the actual argument because it only has access to a copy. When passing by reference-to-const, the called function can't store into the actual argument because it's const . Last month, I explored the similarities between pointer and reference initialization.
I have been taught / learned that non-trivial function arguments (i.e. most non-primitive types) should preferably be passed by const reference- quite a few books I've read recommend this as a "best practice".
When an argument is passed by value it is modifiable and copying it may be elided. For example, the canonical way to implement the assignment operator looks like this:
T& T::operator= (T value) {
value.swap(*this);
return *this;
}
At first sight it may look inefficient because a T
is being copied. However, it would be copied anyway, i.e., if a copy is needed one will be created either way:
T& T::operator= (T const& value) {
T(value).swap(*this); // has to do a copy right here
return *this;
}
However, for the first version, it may be possible not to create copy at all, for example
T f() { return T(); }
// ...
T x = ...;
x = f();
When assigning the result of f()
which is of type T
to x
the compiler may decide that it doesn't need to copy the result of f()
and instead pass it into the assignment operator directly. In that case, if the assignment operator takes the argument by const&
the compiler has to create a copy inside the assignment operator. In the implementation taking the argument by value it can elide the copy! In fact, the return from f()
can already elide the copy, i.e., the call to f()
and the following assignment may just involve the default construction of the object! ... and for many modern compilers that is, indeed, the case!
Put differently: if you need to copy an argument, getting it passed by value may avoid the need to create a copy. Also, you can std::move()
from value arguments but not from const&
arguments.
But you can also pass by reference when you want the program to be more efficient and if you don't want to change anything in the variable passed to the function, you just make it const.
It's wrong. Passing the arguments of basic types (int, float, char....) is more effecient than passing them by reference. const & is more effecient in passing the BIG OBJECT. Because the reference is a alias of a object in essential, the compiler need to handle more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With