簡體   English   中英

C# 指針數組

[英]C# array of pointers

我目前正在嘗試在 Unity 中創建一個八叉樹。 我希望每個節點都有一個指向子節點 AABB 的指針列表。 AABB 是一個如下所示的結構:

    public struct ABB
    {
        public float size;

        public float centerX;
        public float centerY;
        public float centerZ;

        public ABB(float size, float centerX, float centerY, float centerZ)
        {
            this.size = size;
            
            this.centerX = centerX;
            this.centerY = centerY;
            this.centerZ = centerZ;
        }
    }

每個節點都包含其子節點的列表:

 public OctreeNode[] children = null;

我想在突發作業中使用節點子節點的 AABB,但 OctreeNodes 本身是 class,突發不支持。 因此,我想獲得 AABB,但為每個節點存儲它們的副本似乎有點多余。 因此,存儲指向孩子的 AABB 的指針對我來說似乎是最好的解決方案。 但是,我無法理解如何在 C# 中執行此操作。

目前,我有一個指針數組

public ABB*[] childBounds = null;

我的問題是,如何用指針填充這個數組?

就像是:

    if (children[0] == null)
    {
        children[0] = new OctreeNode(new ABB(childLength, nodeBounds.centerX + -quarter, nodeBounds.centerY + quarter, nodeBounds.centerZ + -quarter), this, rootSeed);
        childBounds[0] = children[0].nodeBounds;
    }

給我一個錯誤,基本上是說

無法將類型 AABB (children[0].nodeBounds) 隱式轉換為 AABB*

有人可以嘗試在 C# 中解釋如何正確執行此操作嗎?

感謝評論中為我指明正確方向的所有人。

本質上,將指針存儲在臨時變量中,然后在 fixed() 語句中對其進行賦值,如下所示:

        fixed(ABB* boundsPtr = &children[0].nodeBounds)
        {
            childBounds[0] = boundsPtr;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM