Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass by reference/value overload

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?

like image 333
kchpchan Avatar asked Feb 07 '15 12:02

kchpchan


People also ask

What is the advantage of passing by reference?

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.

What is meant by pass by reference?

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.

Why do we pass by reference in C++?

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.

How do you overload an assignment operator?

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.


2 Answers

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.

like image 125
Sergey Kalinichenko Avatar answered Oct 24 '22 00:10

Sergey Kalinichenko


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.

like image 44
emlai Avatar answered Oct 24 '22 00:10

emlai