C# 托管非托管代码
我试图理解托管/非托管代码,因为它涉及结构和类。我有一个结构体,具有另一个结构体的属性,但它是一个指针声明,如下所示:
struct StateInfo
{
Bitboard board;
StateInfo* previous;
}
我正在将 C++ 项目转换为 C#。无论如何,这不起作用,因为 Bitboard 是一个类。我得到的错误是因为无法在托管类型上声明指针。如果我从结构中取出 Bitboard,那就没问题了。不过我需要它,所以我将 Bitboard 从类更改为结构,一切都很好。我不知道怎么了?有什么想法吗?
I'm trying to understand managed/unmanaged code as it pertains to structs and classes. I have a struct with a property of another struct but its a pointer declaration as in:
struct StateInfo
{
Bitboard board;
StateInfo* previous;
}
I'm converting a C++ project to C#. Anyways, this doesn't work because Bitboard is a class. The error I get is something to the fact that pointers cannot be declared on managed types. If I take out Bitboard from the struct, it's fine. I need it though so I changed Bitboard from a class to a struct, and all is good. I'm not sure what's up? Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
本质上,在 C# 中,所有对象都自动成为指针,不需要释放。
尝试阅读一些有关从 C++ 迁移到 C# 的过渡文章
C++ -> C#:从 C++ 迁移到 C# 需要了解什么
Essentially, in c# all objects are automatically a pointer and do not need to be released.
Try reading some transitional articles about moving from C++ to C#
C++ -> C#: What You Need to Know to Move from C++ to C#
我建议您阅读bittability。
Blittable 类型在托管和非托管代码中具有相同的二进制表示形式,如果您希望指针有意义,则需要以相同的方式表示它们。
I suggest you read about blittability.
Blittable types have the same binary representation in managed and unmanaged code, and you need to represent them the same way if you want pointers to make sense.
您可能甚至不需要
struct
。相反:在 C# 中,
struct
是一种值类型。例如,int
是一个struct
。它们通常应该用于完全由其值描述的事物。You probably don't even want a
struct
. Instead:In C#, a
struct
is a value type. For instance,int
is astruct
. They should typically be used for things which are entirely described by their value.