Immortal Nephi said:
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.
So, if I understand you, your RGB8 type will be able to handle 4 reds, 8
blues and 8 greens. OK.
The array class has more data members like that.
class Array {
¡..
¡..
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;
};
Please correct me if I am wrong, but all your members of type int
(except m_Size) and unsigned char are logically constants? I would also
suggest, if you wish them to be members of your class Array, it would be
better if the were static const members, and also that they were
public. I do not say that they *must* be so, but the sense that I have
of how you would like to use them, suggests that they are part of the
interface of class Array. Also, if they are so, then you may even be
able to avoid what you are calling the `bidirectional communication'
between Array and RGB8. As I understand it, your reason for wanting to
pass a reference to your Array instance in initializing your Byte return
value in Array:
p[] is merely to have access later to these members of
Array. If, however, they are indeed const and are declared public and
static, then you would not have the need for this bidirectional
communication at all (which, I have to say, I do not like).
One other point. Though I can only guess at the purpose of the
m_... members of Array above, I would have thought that the masks should
be:
m_RedMask = 0xC0 (1100 0000)
m_GreenMask = 0x38 (0011 1000)
m_BlueMask = 0x07 (0000 0111)
given how you describe them above. Here I am supposing that you want
the mask to do things like:
unsigned char aColor = anArray[0];
unsigned char redOnly = aColor & Array::m_RedMask;
unsigned char greenOnly = aColor & Array::m_GreenMask;
unsigned char blueOnly = aColor & Array::m_BlueMask;
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.
You do not say what ShiftRed_Ref and ShiftGreen_Ref are. I can only
guess that they are references to Array::m_ShiftRed and
Array::m_ShiftGreen. If that is the case then they are also constants
and are better being public static const members of Array so that you
don't need to pass these at all.
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:
perator[](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.
Here you speak about "read[ing] some other Array's data members through
bidirectional communication." If you are referring to the members of
Array that you have shown already, I hope that I have already given you
an idea as to how you might avoid your `bidirectional communication'
altogether, which I would suggest is the right thing to do, if I have
understood you correctly.
By "Array object's operator[] uses Byte's member function to modify
pData," do you mean something like you asked for in an earlier post,
like:
array[0].Set_HighNibble(...).Set_LowNibble
?
Please let me know if bidirectional communication is a good idea. If
not, what are you suggesting alternative? I like proxy Byte class.
To summarise, assuming that I have understood you correctly, I would
suggest that `bidirectional communication' - in the sense that
Array:
p[] returns a Byte object which holds a reference to the Array
object - is not a good idea. As I have shown, it is probably better
that the members of Array that are needed by Byte objects are logically
constant and can be public static members of the interface of Array.
Regards
Paul Bibbings