Re: Can we override [][] ?
 
"Tom1s" <No.Email@Address> wrote in message 
news:Tl6fg.9753$j7.306779@news.indigo.ie...
Jim Langston posted:
I would need an example please, as I don't know how to override[] to
accept two paramters but you seem to indicate it is two instances of
[].  What is the 'right thing' or can you direct me to a web page on
the subject?  As I said, in this case Google was not my friend.
I only spent fifteen minutes on this, so it's by no means perfect.
Untested code:
class ChessBoard {
public:
   class Square {
   public:
       enum SquareContents {
           empty, pawn, castle, horse, bishop, queen, king } contents;
       Square &operator=( SquareContents const sc )
       {
           contents = sc;
           return *this;
       }
   };
   Square squares[64];
   class HorizontalCoordinate {
   private:
       ChessBoard &cb;
       unsigned const x;
   public:
       HorizontalCoordinate(ChessBoard &arg_cb, unsigned const arg_x)
         : cb(arg_cb), x(arg_x) {}
       Square &operator[]( unsigned const y )
       {
           return cb.squares[ x * 8 + y  ];
       }
   };
   HorizontalCoordinate operator[](unsigned const x)
   {
       return HorizontalCoordinate(*this, x);
   }
};
int main()
{
   ChessBoard board;
   board[3][5] = ChessBoard::Square::bishop;
}
As you can see, a simple member function (or even function-style
operator) would probably be better.
Thank you.  Works well.  This is my solution if anyone is interested.
union SPixel
{
    struct {
        unsigned char B, G, R, A;
    };
    unsigned long Value;
};
class CMyBitmap
{
public:
    CMyBitmap( int Rows, int Columns ): Rows_( Rows ), Columns_( Columns )
    {
        Data_ = new SPixel[ Rows * Columns ];
    }
    ~CMyBitmap()
    {
        delete[] Data_;
    }
    SPixel& Pixel( const int Row, const int Column )
    {
        if ( Row > Rows_ || Row < 0 )
            throw std::string( "Out of bounds for row" );
        else if ( Column > Columns_ || Column < 0 )
            throw std::string( "Out of bounds for column" );
        else
            return Data_[ Columns_ * Row + Column ];
    }
    class HorizCoord
    {
    public:
        HorizCoord( CMyBitmap& Bitmap, const int Row ): Bitmap_( Bitmap ), 
Row_( Row ) {}
        SPixel& operator[]( const int Column )
        {
            if ( Column > Bitmap_.Columns_ || Column < 0 )
                throw std::string( "Out of bounds for column" );
            else
                return Bitmap_.Data_[ Bitmap_.Columns_ * Row_ + Column  ];
        }
    private:
        CMyBitmap& Bitmap_;
        const Row_;
    };
    HorizCoord operator[] (int const Row)
    {
        if ( Row > Rows_ || Row < 0 )
            throw std::string( "Out of bounds for row" );
        else
            return HorizCoord(*this, Row);
    }
private:
    SPixel* Data_;
    int Rows_;
    int Columns_;
    // No copy or assignment yet so disable by making private.
    CMyBitmap ( CMyBitmap const& ) {};
    CMyBitmap& operator=( CMyBitmap const& ) {};
};
int main()
{
    SPixel Pixel;
    Pixel.Value = 0x01020304;
    std::cout << "0x01020304 by color: " << (int) Pixel.A << " " << (int) 
Pixel.R << " " << (int) Pixel.G << " " << (int) Pixel.B << std::endl;
    CMyBitmap BitMap( 100, 100 );
    BitMap.Pixel(10, 10).R = 255;
    BitMap.Pixel(10, 10).B = 255;
    BitMap.Pixel(10, 10).G = 255;
    BitMap.Pixel(10, 10).A = 255;
    std::cout << "All bits set by value: " <<BitMap.Pixel(10, 10).Value << 
std::endl;
    BitMap[15][15].R = 128;
    BitMap[15][15].B = 129;
    BitMap[15][15].G = 130;
    BitMap[15][15].A = 131;
    std::cout << "Set using [][]: " << BitMap.Pixel(15, 15).Value << 
std::endl;
    BitMap.Pixel(20, 20).Value = 0xFFFF0000;
    std::cout << "0xFFFF0000 by color: " << (int) BitMap.Pixel(20, 20).A << 
" " << (int) BitMap.Pixel(20, 20).R << " " << (int) BitMap.Pixel(20, 20).G 
<< " " << (int) BitMap.Pixel(20, 20).B << std::endl;
    // Lets just prove that Intel is Bigendian.
    unsigned char* pchar = reinterpret_cast<unsigned char*>( 
&(BitMap.Pixel( 20, 20 ).Value ) );
    std::cout << "First Byte:" << (int) *(pchar++) << std::endl;
    std::cout << "Second Byte:" << (int) *(pchar++) << std::endl;
    std::cout << "Third Byte:" << (int) *(pchar++) << std::endl;
    std::cout << "Fourth Byte:" << (int) *pchar << std::endl;
    std::string wait;
    std::cin >> wait;
}