简体   繁体   English

非常量左值引用的示例

[英]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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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