0

C# で ref キーワードを使用して、デリゲート関数に渡されるクラス変数を変更しようとしています。親とその 2 つの子のコンテナーに格納されている値をデリゲート関数で変更できるようにしたいと考えています。現在起こっていることは、デリゲート関数は親を変更できますが (container[parent] に直接参照を渡すため)、子は変更できません。最初にそれらを処理して、leftChild と rightChild への参照を渡す必要があるためです。

  • デリゲート関数がコンテナに格納されている値を変更できるように、leftChild をコンテナ [leftChildIndex] への参照にすることは可能ですか? (右の子も同様)

    private void traversePostOrder(Modify operation, int parentIndex) {
        if (parentIndex < size) {
            int leftChildIndex = getLeftChildIndex(parentIndex);
            int rightChildIndex = getRightChildIndex(parentIndex);
            T parent = container[parentIndex];
            T leftChild = default(T);
            T rightChild = default(T);
    
            Library.Diagnostics.Message.logMessage("P: " + parent, 2);
    
            if (leftChildIndex < container.Length) {
                traversePostOrder(operation, leftChildIndex);
                leftChild = container[leftChildIndex];
            }
    
            if (rightChildIndex < container.Length) {
                traversePostOrder(operation, rightChildIndex);
                rightChild = container[rightChildIndex];
            }
    
            operation(ref container[parentIndex], ref leftChild, ref rightChild);
        }
    }
    
4

2 に答える 2

1

問題は、それらを定義する場所です。

T leftChild = default(T);
T rightChild = default(T);

これらのオブジェクトへの参照を渡すと、それらはローカル変数であるため、メソッドの終了直後に破棄されます。
オブジェクトを直接送信してみてください。

private void traversePostOrder(Modify operation, int parentIndex) {
    if (parentIndex < size) {
        int leftChildIndex = getLeftChildIndex(parentIndex);
        int rightChildIndex = getRightChildIndex(parentIndex);
        T parent = container[parentIndex];
        bool leftChildModified = false;
        bool rightChildModified = false;

        Library.Diagnostics.Message.logMessage("P: " + parent, 2);

        if (leftChildIndex < container.Length) {
            traversePostOrder(operation, leftChildIndex);
            leftChildModified = true;
        }

        if (rightChildIndex < container.Length) {
            traversePostOrder(operation, rightChildIndex);
            rightChildModified = true;
        }

        if(leftChildModified && rightChildModified)
        {
            operation(ref container[parentIndex], ref container[leftChildIndex], ref container[rightChildIndex]);
        }
        else if(leftChildModified)
        {
            operation(ref container[parentIndex], ref container[leftChildIndex], ref Default(T));
        }
        else if(rightChildModified)
        {
            operation(ref container[parentIndex], ref Default(T), ref container[rightChildIndex]);
        }
        else
        {
            operation(ref container[parentIndex], ref default(T), ref default(T));
        }
    }
}
于 2011-10-18T15:05:33.473 に答える
1

あなたが探しているのはポインターであり、C# はそれらを公開していません - 幸いなことに。

値をクラス変数に割り当てるだけです。

operation(ref container[parentIndex], ref leftChild, ref rightChild);
container[leftChildIndex] = leftChild;
container[rightChildIndex] = rightChild;
于 2011-10-18T15:11:18.707 に答える