C# 托管非托管代码

发布于 2024-11-09 06:23:17 字数 309 浏览 5 评论 0原文

我试图理解托管/非托管代码,因为它涉及结构和类。我有一个结构体,具有另一个结构体的属性,但它是一个指针声明,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

懵少女 2024-11-16 06:23:48

本质上,在 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#

秋叶绚丽 2024-11-16 06:23:41

我建议您阅读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.

烟火散人牵绊 2024-11-16 06:23:36

您可能甚至不需要struct。相反:

class StateInfo
{
   Bitboard board;
   StateInfo previous;
}

在 C# 中,struct 是一种值类型。例如,int 是一个struct。它们通常应该用于完全由其值描述的事物。

You probably don't even want a struct. Instead:

class StateInfo
{
   Bitboard board;
   StateInfo previous;
}

In C#, a struct is a value type. For instance, int is a struct. They should typically be used for things which are entirely described by their value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文