Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
C++
#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:
C++
#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
        .
        .
        .
    }
}

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.
Posted

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900