Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

c++ cli - unmanaged var as member of managed class c++

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...