Re: Operator Cast () Reference?
On Jun 10, 9:23 am, Paul Bibbings <paul.bibbi...@gmail.com> wrote:
Immortal Nephi <Immortal_Ne...@hotmail.com> writes:
Someone posted his Byte class code on the previous thread. I =
have a
question about operator cast ().
I think that would be me.
Please explain the difference
between local object and reference object. Why do you need reference
object?
If you remember, the Byte class was introduced as a `proxy' for your
Array class, which stored a pointer to an array of unsigned char. A
Byte instance was returned by your Array::op[], so:
Byte Array::operator[](int index) { return pData[index]; }
It is created from pData[index] using the non-explicit constructor:
Byte::Byte(unsigned char&);
You explain very clear. Look very good. I want to add. I might
want to add more variables to the Byte's constructor. I may want to
name Byte to be RGB8 (Red, Green, and Blue for pixels ).
RGB8 is limited to 8 bit. Red has 2 bits. Green and blue have 3
bits.
The array class has more data members like that.
class Array {
=85..
=85..
private:
friend class Byte;
unsigned char *m_Data;
int m_RedMax;
int m_GreenMax;
int m_BlueMax;
int m_ColorMax;
int m_ShiftRed; // m_ShiftRed = 6
int m_ShiftGreen; // m_ShiftGreen = 3
unsigned char m_RedMask; // m_RedMask = 0x3F
unsigned char m_GreenMask; // m_GreenMask = 0xC3
unsigned char m_BlueMask; // m_BlueMask = 0xF8
int m_Size;
};
You can always write four parameters in Constructor function
according to your preference.
I have one problem. I want to add ShiftRed_Ref and ShiftGreen_Ref to
Byte constructor. I have no reason to add three or more parameters in
Byte constructor, but two parameters should be sufficient. First
parameter is to be data_Ref and second parameter is to be Array_Ref.
I will need to establish bidirectional reference between Array object
and Byte object.
Byte Array::operator[](int index) {
Byte byte( pData[ index ], *this );
return byte;
}
Notice friend class Byte. Array object's operator[] uses Byte's
member functions to modify pData and read some other Array's data
members through bidirectional communication.
Please let me know if bidirectional communication is a good idea. If
not, what are you suggesting alternative? I like proxy Byte class.
You will see that it stores the unsigned char at pData[index] *by
reference*. This is the whole point of using the proxy. Basically it
`represents' the stored data so that any operation upon the proxy is
*really* an operation upon the data it represents (stores).
The reason that you need:
Byte::operator unsigned char&();
and not:
Byte::operator unsigned char();
is that you need to allow assignment to the underlying unsigned char
through a Byte proxy. Consider:
Array array(4);
// ...
array[0] = 42U;
The last call is equivalent to:
Byte b(array[0]);
b.operator unsigned char&() = 42U; // LHS returns reference t=
o array[0]
Using a reference here 42U gets assigned to the actual unsigned char at
array[0]. If you consider the alternative, which would be:
Byte b(array[0]);
b.operator unsigned char() = 42U; // LHS returns *temporary*
=
// array[0] *not* changed
then what you would be attempting to do is assigne 42U to a *temporary*
returned by b.operator unsigned char(), which is not what you want.
Make sense!