1

I am learning c++ and I think everything is fine till now. I read about references and did some exercises(simple). But there is an understanding problem with this piece of code:

const ints GetMax (const int& valuel, const int& value2).

Could anyone help me understand why it is passing references with the function name and what it will do(also in the type of its arguments)? What will reference do in this case???

2 Answers2

6

A reference in C++ is exactly that, it's a variable name that refers to some other variable.

Think in terms of the statements:

int xyzzy  = 1;
int &plugh = xyzzy;
int twisty = xyzzy;

The actual "object" here for xyzzy is the thing containing the value 1. You refer to it by its name xyzzy but that's really just a name for it.

The plugh is another reference to exactly the same underlying object - changing the value of plugh will also change the value of xyzzy since those are both names of (references to) the same thing.

The twisty variable, on the other hand, is created as a new object and simply copies the value of xyzzy across to it.


You'll often see things like:

void fn(const string &str1) { ... }

in code since it's more efficient to pass a reference to an object that's relatively expensive to construct/copy (like a string). Passing a reference to the object allows you direct access to the currently existing object, and making it const prevents the function from changing it.

It's very unusual to see this done with a basic type like int since the cost of duplicating them is very small, usually about the same as the cost of passing a reference to them.

You will occasionally see things like:

void fn(int &thing) { ... }

but that's usually because the thing is expected to be changed in the function and that change get mirrored back to the caller.


What your particular code is probably doing (though I can't be sure without more context) is not creating any new object, by virtue of the return of a reference. The following code shows, in my best guess, the sort of implementation it would have:

const int& GetMax(const int& value1, const int& value2) {
    if (value1 >= value2)
        return value1;
    return value2;
}

With that, the code:

int val1 = 7;
int val2 = 42;
const int &biggest = GetMax (val1, val2);

will actually set the reference biggest to refer to the largest of val1 and val2. In other words, it's functionally equivalent to the pseudo-code:

val1 = 7
val2 = 42
if (val1 > val2)
    biggest = reference to val1
else
    biggest = reference to val2
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • thnx I got it, but what will reference do if we write it with type name like ***int&***? – Firdavsbek Narzullaev Jul 10 '17 at 04:53
  • 1
    But is there any advantage in passing `const int&` in parameter? Since it is `int` it does not provide any advantage regarding data coping and since it is `const` we can't change it. So what is the point of passing `const int&` instead of plain `int`? – taskinoor Jul 10 '17 at 05:03
  • 2
    @taskinoor: I've updated my answer based on the new info in the question. Because you want to return a *reference* to a variable, you need to use one whose lifetime extends beyond the function. In other words, pass-by-value parameters and locals are out of the equation. – paxdiablo Jul 10 '17 at 05:05
  • @paxdiablo could you suggest better way of learning c++ please?(Or maybe there are some books which are easy to understand) – Firdavsbek Narzullaev Jul 10 '17 at 05:14
  • Sorry, @FirdavsbekNarzullaev, I suspect someone who's been using C++ for something like quarter of a century is probably the *least* qualified to advise on how to learn it :-) – paxdiablo Jul 10 '17 at 05:16
  • okay) thnx anyway! – Firdavsbek Narzullaev Jul 10 '17 at 05:18
  • *"In other words, it's functionally equivalent to:..."* Not at all. You can't rebind a reference. Also you can't return a const reference and bind it to a non-const reference. – MikeMB Jul 10 '17 at 06:04
  • ...or for ex machine code programmers... "an index register that can't be changed" – Richard Hodges Jul 10 '17 at 06:59
  • @FirdavsbekNarzullaev Grab a [good book, like one our list](https://stackoverflow.com/q/388242/1782465). – Angew is no longer proud of SO Jul 10 '17 at 07:13
1

If you don't pass int by reference, then it can't be modified from inside the function. Copies of the ints would be modified instead.

Cpp plus 1
  • 990
  • 8
  • 25