参考arg

编程入门 行业动态 更新时间:2024-10-28 10:35:40
本文介绍了参考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

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

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

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 itsarguments will reside on the managed heap or on the stack. It needs tohandle 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

本文发布于:2023-07-31 15:48:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1260079.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:arg

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!