非托管var作为托管类c ++的成员 [英] unmanaged var as member of managed class c++
问题描述
我是新手在.net 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
};
但MSV说:
error C4368: cannot define 'klienty' as a member of managed 'Klient': mixed types are not supported
此代码有什么问题?
推荐答案
您可以将.NET基本数据类型作为成员你的托管类(static int i),或指向非托管的任何指针(DWORD * pid,HANDLE * handle),但是你不允许直接有一个非托管对象,整数数组作为一个非托管对象目的。
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
};
这篇关于非托管var作为托管类c ++的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!