unmanaged var as member of managed class c++

让人想犯罪 __ 提交于 2019-11-28 08:09:36

问题


I'm novice in .net c++ and trying to create class looking like:

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's wrong with this code?


回答1:


You can have .NET basic data types as members of your managed class (static int i), or pointers to anything unmanaged (DWORD* pid, HANDLE* handle), but you're not allowed to have an unmanaged object directly, and the array of integers counts as an unmanaged object for this purpose.

Since the only item giving you a problem here is the unmanaged array, you could 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
};

Or, you can declare a unmanaged class, put whatever you need to in there, and have a pointer to it from the managed class. (If you do this in a non-static context, don't forget to delete the unmanaged memory from your finalizer.)

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
};


来源:https://stackoverflow.com/questions/11588261/unmanaged-var-as-member-of-managed-class-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!