相关疑难解决方法(0)

如何在C++(Newbie)中通过引用传递"literal"整数

编辑:正如许多人所指出的,传递引用通常不适合作为原始类型的优化.这很好知道,谢谢大家!即便如此,我的问题更多的是为什么文字价值似乎无法通过引用传递,这已被公认的答案所解决.干杯!


(原谅我的天真:我对C++很陌生.)

为了避免在调用函数(比如"fillRect")时按值复制效率低下,我想通过引用传递参数.

如果我提供参数作为声明的局部变量,它工作正常.但是如果我提供任何"文字"整数,我会得到一个编译错误(没有匹配的函数).

void fillRect( int &x, int &y, int &width, int &height )
{
    // do something
}

int x=10, y=20, w=100, h=80;

fillRect(x, y, w, h); // this compiles and works!
fillRect(x, y, 100, 80); // but this doesn't compile ... why?
Run Code Online (Sandbox Code Playgroud)

是什么赋予了?

c++ pass-by-reference

15
推荐指数
4
解决办法
1万
查看次数

标签 统计

c++ ×1

pass-by-reference ×1