Two proxy classes in main class
I created two private proxy classes and I put them into one main
class. I use two functions Low_Byte() and High_Byte() to modify main
class' data member through reference and Word() through pointer.
C++ Compiler complied without any problems. If I declare constant,
then C++ Compiler will give you an error message.
How can I add const function? I struggle while I am trying to figure
for couple hours.
class _16_Bits {
private:
typedef unsigned int size;
class _Low_Byte {
public:
_Low_Byte( size &byte ) : m_data( byte ) {}
~_Low_Byte() {}
_Low_Byte &operator= ( const size &byte ) {
m_data &= 0xFF00;
m_data |= byte & 0xFF;
return *this;
}
operator size () { return m_data & 0xFF; }
operator size () const { return m_data & 0xFF; }
private:
size &m_data;
};
class _High_Byte {
public:
_High_Byte( size &byte ) : m_data( byte ) {}
~_High_Byte() {}
_High_Byte &operator= ( const size &byte ) {
m_data &= 0xFF;
m_data |= ( byte & 0xFF ) << 8;
return *this;
}
operator size () { return ( m_data >> 8 ) & 0xFF; }
operator size () const { return ( m_data >> 8 ) & 0xFF; }
private:
size &m_data;
};
public:
explicit _16_Bits( size word ) : m_data( word ) {}
~_16_Bits() {}
_16_Bits &operator= ( size word ) {
m_data = word & 0xFFFF;
return *this;
}
_Low_Byte Low_Byte() { return _Low_Byte( m_data ); }
_High_Byte High_Byte() { return _High_Byte( m_data ); }
_16_Bits &Word() { return *this; }
operator size () { return m_data; }
operator size () const { return m_data; }
private:
size m_data;
};
int main () {
_16_Bits W( 0x1234 );
W.Low_Byte() = 0x5A;
W.High_Byte() = 0x7C;
unsigned int L = W.Low_Byte();
unsigned int H = W.High_Byte();
W.Word() = 0xABCD;
unsigned int W2 = W;
const _16_Bits V( 0x1234 );
unsigned int L2 = V.Low_Byte(); // error
unsigned int H2 = V.High_Byte(); // error
unsigned int V2 = V;
return 0;
}