通过引用传递的问题
我可以重载采用引用名或变量名的函数吗?
例如,当我尝试这样做时:
void function(double a);
void function(double &a);
我希望该函数的调用者能够执行以下操作:
double a = 2.5;
function(a); // should call function(double &a)
function(2.3); // should call function(double a)
我想编写按引用传递函数,以更好地使用内存并可能在范围之外操作变量,但不必须创建一个新变量才能调用该函数。
这可能吗?
干杯
Can I overload a function which takes either a reference or variable name?
For example when I try to do this:
void function(double a);
void function(double &a);
I would like the caller of this function to be able to do:
double a = 2.5;
function(a); // should call function(double &a)
function(2.3); // should call function(double a)
I would like to write pass-by-reference functions for better memory use and possible manipulation of the variable outside of scope, but without having to create a new variable just so I can call the function.
Is this possible?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为你没有抓住重点。你真正应该拥有的就是这个:
注意“const”。这样,您应该始终获得引用传递。如果您有非常量引用传递,那么编译器将正确地假设您希望修改传递的对象 - 这当然在概念上与按值传递变体不兼容。
使用 const 引用,编译器会很乐意在你背后为你创建临时对象。非 const 版本不适合您,因为编译器只能将这些临时对象创建为“const”对象。
I think you're missing the point here. What you really should have is JUST this:
Note the "const". With that, you should always get pass-by-reference. If you have non-const pass by reference, then the compiler will correctly assume that you wish to modify the passed object - which of course is conceptually incompatible with the pass-by-value variant.
With const references, the compiler will happily create the temporary object for you behind your back. The non-const version doesn't work for you, because the compiler can only create these temporaries as "const" objects.
我尝试过,但失败了
至少在 MSVC2008 中,这是不可能的 - 而且我认为这适用于所有 c++ - 编译器。
定义本身是有效的,但是当尝试使用
变量作为参数调用函数时,会引发编译器错误,因为编译器无法决定使用哪个函数。
I tried it, and it failed
At least in MSVC2008, this is not possible - and I think this applys to all c++ - Compilers.
The definiton itself is valid, but when trying to call the function
with a variable as a parameter, an compiler error is raised as the compiler is unable to decide which function to use.
我不认为定义:
是可能的...编译器如何知道为
function(x)
调用哪个函数?如果您想同时使用“按引用”和“按值”,则应使用两个不同的函数。与这种模棱两可的重载相比,这也将使代码更容易阅读。
I don't think the definition:
is possible... How would the compiler know which function to call for
function(x)
?If you want to use both by Reference and by Value, you should use two different functions. This would also make the code much easier to read, than this ambiguous overload.
通过引用传递 POD 类型没有任何好处,因为它们几乎总是(总是?)在注册表中传递。
通过 const 引用传递的复杂类型(由多个 POD 变量组成的类型)通常(总是?:D)是比复制和堆栈推送更可取的方式。
您甚至可以通过创建 显式构造函数
There is no benefit of passing POD-types by reference, as they are almost always (always?) passed in a registry.
Complex types (ones that consist of more than just one POD-variable) passed by const reference is usualy (always?:D) a prefferable way over a copying and stack push.
You even can controll the creation of temporaries of your complex-typed variables from POD-types by creating explicit constructors
不,您不能这样做,因为这两个函数将有效地产生相同的损坏名称,并且链接器将无法解析函数调用。
No, you cannot do this coz both the functions will effectively produce the same mangled names and the linker would be unable to resolve the function call.