gen - 1 month ago 5

C++ Question

If I have two variables,

`a`

`b`

`std::max(a,b)`

Is it somehow possible to have this function

`x`

`max(a,b) = x;`

such that after this call

`a==x`

`a`

`b`

`b==x`

Answer

Maybe you want this:

```
int &max(int &a, int &b)
{
return a > b ? a : b;
}
```

Then:

```
int main()
{
int x = 10, y = 20;
max(x, y) = 100;
}
```

`max`

will return a reference to the maximum number, then you can put your `max`

function in the left hand of the assignment and change its value.

Template based version:

```
template<typename T>
T &max(T &a, T &b)
{
return a > b ? a : b;
}
```

Source (Stackoverflow)

Comments