Example of a non-const lvalue reference

259 views Asked by At

Can someone given an example of a "non-const lvalue reference"?

I need to pass an object to a routine where the object's state will be modified, after the routine has completed I expect to use the object with the modified state.

I read elsewhere that I am supposed to pass the object as a: "non-const lvalue reference." What is that and can someone give an example?

1

There are 1 answers

1
Vlad from Moscow On

Here you are

#include <iostream>

void increase( int &x )
{
    ++x;
}

int main()
{
    int x = 0;

    std::cout << "x = " << x << std::endl;

    increase( x );

    std::cout << "x = " << x << std::endl;
}