简体   繁体   中英

Example of a non-const lvalue reference

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?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM