Unmanaged var as a member of a C ++ managed class

I am new to .net C ++ and trying to create a class similar to:

public ref class Klient
{
public:
    Klient(){}
    // zmienne
    static DWORD klienty[41][2];
    static int i = 1;
    static DWORD* pid;
    static HANDLE* handle;

    //funkcje
};

but MSV says that:

error C4368: cannot define 'klienty' as a member of managed 'Klient': mixed types are not supported

What is wrong with this code?

+5
source share
1 answer

You can have basic .NET data types as members of your managed class (static int i) or pointers to something unmanaged (DWORD * pid, HANDLE * handle), but you are not allowed to have an unmanaged object directly, but an array of integers for this goal is considered an unmanaged object.

Since the only thing that gives you the problem is an unmanaged array, you can switch it to a managed array.

public ref class Klient
{
public:
    Klient(){}
    // zmienne
    static array<DWORD,2>^ klienty = gcnew array<DWORD,2>(41,2);
    static int i = 1;
    static DWORD* pid;
    static HANDLE* handle;

    //funkcje
};

, , , . ( , .)

public class HolderOfUnmanagedStuff
{
public:
    DWORD klienty[41][2];
    int i;
    DWORD* pid;
    HANDLE* handle;

    HolderOfUnmanagedStuff()
    {
        i = 1;
    }
};

public ref class Klient
{
public:
    Klient(){}
    // zmienne
    static HolderOfUnmanagedStuff* unmanagedStuff = new HolderOfUnmanagedStuff();

    //funkcje
};
+11

All Articles