如何在本机类中注入C ++ / CLI基类ref类。 [英] How to injection C++/CLI base ref class to in native class.

查看:109
本文介绍了如何在本机类中注入C ++ / CLI基类ref类。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在本地其他课程中使用我的基础ref课程。



我的代码是这样的。



这是头文件中的第一个类:

I want to use my base ref class in native other class.

My code is like this.

This is first class in a header file:

#include "SecondNativeClass.h"
using namespace System;
namespace Vrp
{
    public ref class BaseClass
    {
    public:
        ~BaseClass()
        SecondNativeClass* mysec;
        DoSomething()
        {
         mysec = new SecondNativeClass();
        }
        .
        .
        .
    }
}





这是头文件中的第二类:



This is second class in a header file:

#include "AnotherNativeClass.h"
using namespace System;
namespace Vrp
{
    //class BaseClass;
    public class SecondNativeClass : public AnotherNativeClass
    {
    public:
        SecondNativeClass();

//        I have try this
        gcroot<BaseClass^> base;    //-> Returned errors

//        I tried that, but again errors;
        BaseClass* base;             //-> Returned errors

//        I tried that, but again errors;
        BaseClass^ base; //-> Returned errors

//        I tried that like as on MSDN page (Mix Types Safely and Correctly)
        msclr::auto_gcroot<BaseClass^> base; //-> Returned errors
        .
        .
        .
    }
}



如果我写class BaseClass;在SecondNativeClass之上...

这次我得到这个已经定义的BaseClass错误。



所有课程在同一名称空间但是编译器返回错误。



什么是正确定义的代码?

如何解决这个问题?

问候。


If I write "class BaseClass;" on top of SecondNativeClass...
This time I'm getting "this BaseClass already defined" error(s).

All classes in same namespace. But compiler returned errors.

What is right defined code?
How can I fix this problem?
Regards.

推荐答案

你的前向声明必须有 ref 修饰符,因为BaseClass是一个托管类。 />


我认为你不能在本机类上拥有 public 修饰符( SecondNativeClass )。



gcroot auto_gcroot 必须用于本机类中的托管成员。如果对象应自动处理,则需要依赖。
You must have ref modifier on your forward declaration since BaseClass is a managed class.

I don't think you can have the public modifier on a native class (SecondNativeClass).

gcroot or auto_gcroot must be used for a managed member in a native class. Which want depend if the object should be automatically disposed.


这篇关于如何在本机类中注入C ++ / CLI基类ref类。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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