When pass a variable to a function, why the function only gets a duplicate of the variable?

前端 未结 9 1287
野的像风
野的像风 2020-12-06 18:05

When pass a variable to a function, why the function only gets a copy/duplicate of the variable?

int n=1;

void foo(int i)
{
    i++;
}

As

9条回答
  •  [愿得一人]
    2020-12-06 18:29

    If you really want a function to change its actual parameter, you can pass it by reference in C++

    void foo(int& i)
    {
        i++;
    }
    

提交回复
热议问题