Trying to figure out why no overloading-ambiguity caused in the following codes:
float foo2(float& i)
{
cout << "call from reference" << endl;
return i;
}
float foo2(float i)
{
cout << "call from non reference"<<endl;
return i;
}
int main()
{
cout<<foo2(2); // print "call from non reference"
}
The foo2 whose parameters not passed by reference is called. Why? How to call the foo2 that pass reference parameters?
Here are some advantages of passing by reference: No new copy of variable is made, so overhead of copying is saved. This Makes program execute faster specially when passing object of large structs or classes. Array or Object can be pass.
Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the argument by using its reference passed in. The following example shows how arguments are passed by reference.
If you recall, using pass by reference allows us to effectively “pass” the reference of a variable in the calling function to whatever is in the function being called. The called function gets the ability to modify the value of the argument by passing in its reference.
Assignment Operators Overloading in C++ You can overload the assignment operator (=) just as you can other operators and it can be used to create an object just like the copy constructor. Following example explains how an assignment operator can be overloaded.
The
foo2
whose parameters not passed by reference is called. Why?
Because you cannot pass a constant or any computed expression by reference to a function that takes a non-constant reference. To pass an expression by reference you need an assignable value - something that can appear on the left-hand side of an assignment expression. Since 2
cannot appear on the left side of an assignment expression, it cannot be used to call a function that expects a reference. When the reference is const
, you can pass anything, because C++ will create a temporary variable, assign the expression to it, and pass a reference to function taking const
reference.
How to call the
foo2
that pass reference parameters?
There is no obvious way of doing that, because the moment you pass a variable or another expression that can become a reference, the compiler will complain that you are making an ambiguous call:
float f;
foo2(f); // <<== This will not compile
There is a way to call it, though: you can make a function pointer that matches only one of the two function signatures, and use it to make your call:
typedef float (*fptr_with_ref)(float&);
int main()
{
cout<<foo2(2) << endl; // print "call from non reference"
fptr_with_ref foo2ptr(foo2); // Only one overload matches
float n = 10;
cout<<foo2ptr(n) << endl; // Calls foo2(float&)
}
Demo.
The 2
you gave as a parameter for foo2
is an rvalue, which cannot be a reference. Thus the function accepting a reference cannot be called with it.
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