非托管var作为托管类c ++的成员

编程入门 行业动态 更新时间:2024-10-28 14:34:07
本文介绍了非托管var作为托管类c ++的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是新手在 c + +,并尝试创建类看起来像:

I'm novice in 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 ++的成员

本文发布于:2023-11-06 04:29:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562720.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:成员   var

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!