tags:

views:

287

answers:

4

I have a class called ImageMatrix, which implements the C++ map in a recursive fashion; the end result is that I have a 3 dimensional array.

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;

Each operator simply returns a map-wrapper class within, like so:

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

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

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

Basically, when I set the value as say 100, and test the value to cout, it shows as 0, and not the number to which I had set it.

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;
  }
 }
}

Note: I've altered the original code for this example so that it takes up less space, so some syntax errors may occur (please don't point them out).

+1  A: 

All your operator[] functions except one return values - they should all return references.

anon
+5  A: 

The following operators return by value, no writes modify the actual data.

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

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

Use:

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


ImageMatrixColumn& ImageMatrixRow::operator[](VUInt32 columnIndex)
dirkgently
Yep, and his code only compiles by chance because he remembered to return a reference from ImageMatrixColumn::operator[]
Greg Rogers
Thanks, how embarrassing! I honestly thought that I was returning references... should have paid more attention at university :p
nbolton
+1  A: 

Your ImageMatrixRow and ImageMatrixColumn operator[]() methods return copies, not referecencs.

Michael Burr
A: 

"Each returns a reference" - are you sure about that?

They look like they return copies of the stored maps, not references to them.

Try, for example:

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

Note the & symbol.

Daniel Earwicker