参考arg [英] reference arg

查看:52
本文介绍了参考arg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


如果函数arg是对内置类型的引用,是否需要

函数定义来指定是否为引用是一个托管或

非托管对象?如果是这样,这是否意味着需要两个

(否则是重复的)函数才能处理两种类型的对象?例如,


void foo(int __gc& x);

void foo(int __nogc& x);


谢谢

解决方案

Ian,

如果函数arg是对内置的引用在类型中,是否需要
函数定义来指定引用是否是托管或
非托管对象?如果是这样,这是否意味着需要两个
(否则重复)函数才能处理这两种类型的对象?例如,

void foo(int __gc& x);
void foo(int __nogc& x);




那要看。你在这里不是在讨论对象是什么?b $ b b管理/不管理,只是它是由跟踪指针引用

(__gc)还是非跟踪指针(__nogc)。


在许多情况下,两个重载都没有用;如果你正在工作

主要是在管理方面,那么__gc版本是优先的,因为它是其他语言(如C#)可以理解的那个
。 />

-

Tomas Restrepo
**** @ mvps.org


谢谢,但有问题的函数无法知道它的

参数是否会驻留在托管堆上或在堆栈上。它需要

处理两者。那我该怎么办?


Ian Lazarus写道:

谢谢,但有问题的功能没有办法知道它的
参数是否将驻留在托管堆或堆栈上。它需要处理两者。那我该怎么办?




你可以只使用__gc&版本,因为它可以绑定到托管的

和非托管引用。至于你需要做什么,似乎

重载与__gc引用不太一样,因为它与__gc

指针一样。例如,管理扩展为C ++

规范,7.6 __gc指针和重载分辨率中的示例对于引用不起作用

,生成f(i)调用下面的错误,我是一个

unmanaged int:


b.cpp(13):错误C2668:''f' ':对重载函数的模糊调用

b.cpp(6):可能是''void f(int __gc&)''

b.cpp(5) :或''void f(int&)''

尝试匹配参数列表''(int)''


摆脱int&超载,它编译得很好。


-

Doug Harrison

Microsoft MVP - Visual C ++


Hello,

If a function arg is a reference to a built in type, is it necessary for the
function definition to specify whether the reference is to a managed or
un-managed object? If so, does that mean that there needs to be two
(otherwise duplicate) functions in order for both types of objects to be
processed? E.g.,

void foo(int __gc & x);
void foo(int __nogc & x);

Thanks

解决方案

Ian,

If a function arg is a reference to a built in type, is it necessary for the function definition to specify whether the reference is to a managed or
un-managed object? If so, does that mean that there needs to be two
(otherwise duplicate) functions in order for both types of objects to be
processed? E.g.,

void foo(int __gc & x);
void foo(int __nogc & x);



That depends. You''re not talking here about the object being
managed/unmanaged, just whether it is being referenced by a tracking pointer
(__gc) or a non-tracking pointer (__nogc).

In many cases, it won''t be useful to have both overloads; if you''re working
mostly on the managed side, then the __gc version is preffered because its
the one that other languages (like C#) can understand.

--
Tomas Restrepo
to****@mvps.org


Thanks but the function in question has no way of knowing whether its
arguments will reside on the managed heap or on the stack. It needs to
handle both. So what do I do?


Ian Lazarus wrote:

Thanks but the function in question has no way of knowing whether its
arguments will reside on the managed heap or on the stack. It needs to
handle both. So what do I do?



You can get away with just the __gc& version, as it can bind to both managed
and unmanaged references. As for what you need to do, it appears that
overloading doesn''t quite work with __gc references as it does with __gc
pointers. For instance, the example in the "Managed Extensions for C++
Specification", "7.6 __gc Pointers and Overload Resolution", does not work
for references, producing the error below on the f(i) call, i being an
unmanaged int:

b.cpp(13) : error C2668: ''f'' : ambiguous call to overloaded function
b.cpp(6): could be ''void f(int __gc &)''
b.cpp(5): or ''void f(int &)''
while trying to match the argument list ''(int)''

Get rid of the int& overload, and it compiles fine.

--
Doug Harrison
Microsoft MVP - Visual C++


这篇关于参考arg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆