I
Immortal Nephi
Can overloading operator[] be connected to any member functions
through ‘this’ pointer? I do not want to insert assignment between
array object and integer value. I prefer to place integer value in
the member function’s parameter.
For example:
class Array
{
public:
Array( int size ) : pData( new unsigned char[ size ] )
{}
~Array()
{
delete [] pData;
}
int &operator[]( int index )
{
return pData[ index ];
}
Array &Set_LowNibble( unsigned char val, int index )
{
pData[ index ] &= 0xF0;
pData[ index ] |= (val & 0x0F );
return *this;
}
Array &Set_HighNibble( unsigned char val, int index )
{
pData[ index ] &= 0x0F;
unsigned char temp = val;
temp &= 0x0F;
temp <<= 4;
pData[ index ] |= temp;
return *this;
}
private:
unsigned char *pData;
};
int main()
{
Array array( 4 );
array[ 0 ] = 0x1E; // Always valid code
// Not valid code but you find another way
array[ 1 ].Set_LowNibble( 0x0D, 1 ).Set_HighNibble( 0x02, 1 );
return 0;
}
through ‘this’ pointer? I do not want to insert assignment between
array object and integer value. I prefer to place integer value in
the member function’s parameter.
For example:
class Array
{
public:
Array( int size ) : pData( new unsigned char[ size ] )
{}
~Array()
{
delete [] pData;
}
int &operator[]( int index )
{
return pData[ index ];
}
Array &Set_LowNibble( unsigned char val, int index )
{
pData[ index ] &= 0xF0;
pData[ index ] |= (val & 0x0F );
return *this;
}
Array &Set_HighNibble( unsigned char val, int index )
{
pData[ index ] &= 0x0F;
unsigned char temp = val;
temp &= 0x0F;
temp <<= 4;
pData[ index ] |= temp;
return *this;
}
private:
unsigned char *pData;
};
int main()
{
Array array( 4 );
array[ 0 ] = 0x1E; // Always valid code
// Not valid code but you find another way
array[ 1 ].Set_LowNibble( 0x0D, 1 ).Set_HighNibble( 0x02, 1 );
return 0;
}