将本机类型的C ++ / CLI包装器传递到另一个C ++ / CLI组件 [英] Pass a C++/CLI wrapper of a native type to another C++/CLI assembly

查看:345
本文介绍了将本机类型的C ++ / CLI包装器传递到另一个C ++ / CLI组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个NativeClassInstance的简单包装器。

  public ref class Wrapper 
{
private:
NativeClass * _wrapped;
public:
Renderer()
{
_wrapped = new NativeClass();
}
〜Renderer()
{
delete _wrapped;
}
operator NativeClass *()
{
return _wrapped;
}
}



现在,我想创建一个Wrapper的实例C#与 Wrapper wrapper = new Wrapper()并在另一个程序集中的另一个原生功能包装器中使用 Helper .Foo(wrapper)(没有什么奇怪的其他功能不与另一个程序集中的包装类直接相关的IMO):

  //实用程序在另一个程序集
public ref class Helper
{
public:
static Foo(Wrapper ^ wrapper)
{
//在本机代码中使用wrapper-> _wrapped
}执行某些操作
}


$ b b

隐式用户转换的结果是:




  • 候选函数不可访问



如果我使_wrapped public,则是:




  • in class ...



现在,我了解到本机类型的可见性为 private 。所以,我应该使用包装的实体在本地代码之外的程序集定义?我已阅读 make_public ,但你不能使用模板类型,所以在一般情况下似乎非常有限。我缺少什么?是否有更正确的解决方案?

解决方案

如果你使用make_public,你的解决方案_wrapped public应该工作最好做一个公共访问器)。关于你的评论我已经阅读了make_public,但是你不能使用模板类型,所以它在一般情况下似乎非常有限。我同意 - 在这里读取我使用的解决方法:
http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/b43cca63-b0bf-451e-b8fe-74e9c618b8c4/



更多相关信息:
编译器错误的最佳解决方法C2158:make_public不支持原生模板类型



祝你好运! / p>

Suppose I have the following simple wrapper of a NativeClassInstance.

public ref class Wrapper
{
private:
    NativeClass *_wrapped;
public:
    Renderer()
    {
        _wrapped = new NativeClass();
    }
    ~Renderer()
    {
        delete _wrapped;
    }
    operator NativeClass*()
    {
        return _wrapped;
    }
}

Now, I want to create an instance of Wrapper from C# with Wrapper wrapper = new Wrapper() and use it in another native functionalities wrapper that resides in another assembly with Helper.Foo(wrapper) (nothing strange having other functionalities not directly related to the wrapped classes in another assembly, IMO):

// Utilities is in another Assembly
public ref class Helper
{
public:
    static Foo(Wrapper ^wrapper)
    {
        // Do something in native code with wrapper->_wrapped
    }
}

The results with the implicit user conversion is:

  • candidate function(s) not accessible

If I make _wrapped public it is:

  • cannot access private member declared in class ...

Now, I've learnt that native type visibility is private outside of the assembly. So, how I'm supposed to use the wrapped entity in native code outside the assembly it's defined? I've read of make_public but you can't use with template types so it seems very limiting in the general case. Am I missing something? Is there a more correct solution?

解决方案

If you use make_public, your solution of making _wrapped public should work (it would obviously be best to make a public accessor instead). Regarding your comment "I've read of make_public but you can't use with template types so it seems very limiting in the general case." I agree--read here for the workaround I used: http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/b43cca63-b0bf-451e-b8fe-74e9c618b8c4/

More related info: Best workaround for compiler error C2158: make_public does not support native template types

Good luck!

这篇关于将本机类型的C ++ / CLI包装器传递到另一个C ++ / CLI组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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