1

C++ マップを再帰的に実装する ImageMatrix というクラスがあります。最終結果は、3 次元配列を持っていることです。

typedef uint32_t VUInt32;
typedef int32_t VInt32;

class ImageMatrix
{
public:
    ImageMatrixRow operator[](VInt32 rowIndex)
private:
    ImageMatrixRowMap rows;
};

typedef std::map <VUInt32, VInt32> ImageMatrixChannelMap;

class ImageMatrixColumn
{
public:
    VInt32 &operator[](VUInt32 channelIndex);
private:
    ImageMatrixChannelMap channels;
};

typedef std::map<VUInt32, ImageMatrixColumn> ImageMatrixColumnMap;

class ImageMatrixRow
{
public:
    ImageMatrixColumn operator[](VUInt32 columnIndex);
private:
    ImageMatrixColumnMap columns;
};

typedef std::map<VUInt32, ImageMatrixRow> ImageMatrixRowMap;

各演算子は、次のようにマップ ラッパー クラスを返すだけです。

ImageMatrixRow ImageMatrix::operator[](VInt32 rowIndex)
{
    return rows[rowIndex];
}

ImageMatrixColumn ImageMatrixRow::operator[](VUInt32 columnIndex)
{
    return columns[columnIndex];
}

VInt32 &ImageMatrixColumn::operator[](VUInt32 channelIndex)
{
    return channels[channelIndex];
}

基本的に、値を 100 として設定し、値をテストして cout にすると、設定した数値ではなく 0 として表示されます。

for (VUInt32 a = 0; a < GetRowCount(); a++)
{
    for (VUInt32 b = 0; b < GetColumnCount(); b++)
    {
        for (VUInt32 c = 0; c < GetChannelCount(); c++)
        {
            VInt32 value = 100;
            matrix[a][b][c] = value;

            VInt32 test = matrix[a][b][c];

                            // pixel = 100, test = 0 - why?
            cout << pixel << "/" << test << endl;
        }
    }
}

注: この例の元のコードを変更してスペースを節約したため、構文エラーが発生する可能性があります (指摘しないでください)。

4

4 に答える 4

5

次の演算子は値で戻ります。実際のデータを変更する書き込みはありません。

ImageMatrixRow ImageMatrix::operator[](VInt32 rowIndex);

ImageMatrixColumn ImageMatrixRow::operator[](VUInt32 columnIndex);

使用する:

ImageMatrixRow& ImageMatrix::operator[](VInt32 rowIndex)


ImageMatrixColumn& ImageMatrixRow::operator[](VUInt32 columnIndex)
于 2009-03-27T19:36:15.820 に答える
1

1 つの戻り値を除くすべての operator[] 関数 - それらはすべて参照を返す必要があります。

于 2009-03-27T19:36:14.243 に答える
1

andメソッドは参照ではなくコピーImageMatrixRowImageMatrixColumn operator[]()返します。

于 2009-03-27T19:36:51.027 に答える
0

「それぞれが参照を返します」-それについてよろしいですか?

それらは、それらへの参照ではなく、保存されたマップのコピーを返すように見えます。

たとえば、次のようにします。

ImageMatrixRow & ImageMatrix::operator[](VInt32 rowIndex)

&記号に注意してください。

于 2009-03-27T19:37:15.100 に答える