N
Nephi Immortal
I posted my same source code to the previous thread earlier. I ask
you to please read my source code again. It looks neater than before.
I use two pointers of _Word inside either _Low_Byte’s and HighByte’s
assignment operator function, but I do not want to use _Low_Byte’s
pointer or _High_Byte’s pointer because I cannot compare _Low_Byte’s
pointer and _High_Byte’s pointer since both types are different.
Both _Low_Byte’s class and _High_Byte’s class are nested and private
inside _Word class. _Word class has bi-directional communication
through _Word’s pointer.
Please tell me if my source code looks safer, but I feel that it is
tight coupling. I probably want to leave _Low_Byte’s class and
_High_Byte’s class outside _Word’s class so I can design other classes
before other classes can be able to access _Low_Byte’s class and
_High_Byte’s class independently.
Can you please post your example to see if you can find a way to
avoid bi-directional communication because it is highly reported from C
++ Compiler to be undefined classes.
My code is ideal for swaping low byte and high byte in the same
variable back and forth.
class _Word {
private:
typedef unsigned int size;
class _High_Byte;
class _Low_Byte {
public:
_Low_Byte( _Word &word ) : m_word( word ) {}
~_Low_Byte() {}
_Low_Byte &operator=( const _Low_Byte &right ) {
if( &m_word == &right.m_word )
return *this;
m_word.m_data &= 0xFF00;
m_word.m_data |= ( right.m_word.m_data & 0x00FF );
return *this;
}
_Low_Byte &operator=( const _High_Byte &right ) {
if( &m_word == &right.m_word )
return *this;
m_word.m_data &= 0xFF00;
m_word.m_data |= ( ( right.m_word.m_data >> 8 ) & 0x00FF );
return *this;
}
_Word &m_word;
};
class _High_Byte {
public:
_High_Byte( _Word &word ) : m_word( word ) {}
~_High_Byte() {}
_High_Byte &operator=( const _Low_Byte &right ) {
if( &m_word == &right.m_word )
return *this;
m_word.m_data &= 0x00FF;
m_word.m_data |= ( ( right.m_word.m_data << 8 ) & 0xFF00 );
return *this;
}
_High_Byte &operator=( const _High_Byte &right ) {
if( &m_word == &right.m_word )
return *this;
m_word.m_data &= 0x00FF;
m_word.m_data |= ( right.m_word.m_data & 0xFF00 );
return *this;
}
_Word &m_word;
};
public:
_Word( size data ) : m_data( data ) {}
~_Word() {}
_Low_Byte Low_Byte() { return _Low_Byte( *this ); }
_High_Byte High_Byte() { return _High_Byte( *this ); }
private:
size m_data;
};
int main () {
_Word W( 0x1234 ), W2( 0x5678 ), W3( 0x9ABC );
W.Low_Byte() = W2.Low_Byte();
W.High_Byte() = W2.High_Byte();
W.Low_Byte() = W3.High_Byte();
W.High_Byte() = W3.Low_Byte();
W.Low_Byte() = W.Low_Byte(); // Skip Assignment Operator
W.High_Byte() = W.High_Byte(); // Skip Assignment Operator
W.Low_Byte() = W.High_Byte(); // Skip Assignment Operator
W.High_Byte() = W.Low_Byte(); // Skip Assignment Operator
return 0;
}
you to please read my source code again. It looks neater than before.
I use two pointers of _Word inside either _Low_Byte’s and HighByte’s
assignment operator function, but I do not want to use _Low_Byte’s
pointer or _High_Byte’s pointer because I cannot compare _Low_Byte’s
pointer and _High_Byte’s pointer since both types are different.
Both _Low_Byte’s class and _High_Byte’s class are nested and private
inside _Word class. _Word class has bi-directional communication
through _Word’s pointer.
Please tell me if my source code looks safer, but I feel that it is
tight coupling. I probably want to leave _Low_Byte’s class and
_High_Byte’s class outside _Word’s class so I can design other classes
before other classes can be able to access _Low_Byte’s class and
_High_Byte’s class independently.
Can you please post your example to see if you can find a way to
avoid bi-directional communication because it is highly reported from C
++ Compiler to be undefined classes.
My code is ideal for swaping low byte and high byte in the same
variable back and forth.
class _Word {
private:
typedef unsigned int size;
class _High_Byte;
class _Low_Byte {
public:
_Low_Byte( _Word &word ) : m_word( word ) {}
~_Low_Byte() {}
_Low_Byte &operator=( const _Low_Byte &right ) {
if( &m_word == &right.m_word )
return *this;
m_word.m_data &= 0xFF00;
m_word.m_data |= ( right.m_word.m_data & 0x00FF );
return *this;
}
_Low_Byte &operator=( const _High_Byte &right ) {
if( &m_word == &right.m_word )
return *this;
m_word.m_data &= 0xFF00;
m_word.m_data |= ( ( right.m_word.m_data >> 8 ) & 0x00FF );
return *this;
}
_Word &m_word;
};
class _High_Byte {
public:
_High_Byte( _Word &word ) : m_word( word ) {}
~_High_Byte() {}
_High_Byte &operator=( const _Low_Byte &right ) {
if( &m_word == &right.m_word )
return *this;
m_word.m_data &= 0x00FF;
m_word.m_data |= ( ( right.m_word.m_data << 8 ) & 0xFF00 );
return *this;
}
_High_Byte &operator=( const _High_Byte &right ) {
if( &m_word == &right.m_word )
return *this;
m_word.m_data &= 0x00FF;
m_word.m_data |= ( right.m_word.m_data & 0xFF00 );
return *this;
}
_Word &m_word;
};
public:
_Word( size data ) : m_data( data ) {}
~_Word() {}
_Low_Byte Low_Byte() { return _Low_Byte( *this ); }
_High_Byte High_Byte() { return _High_Byte( *this ); }
private:
size m_data;
};
int main () {
_Word W( 0x1234 ), W2( 0x5678 ), W3( 0x9ABC );
W.Low_Byte() = W2.Low_Byte();
W.High_Byte() = W2.High_Byte();
W.Low_Byte() = W3.High_Byte();
W.High_Byte() = W3.Low_Byte();
W.Low_Byte() = W.Low_Byte(); // Skip Assignment Operator
W.High_Byte() = W.High_Byte(); // Skip Assignment Operator
W.Low_Byte() = W.High_Byte(); // Skip Assignment Operator
W.High_Byte() = W.Low_Byte(); // Skip Assignment Operator
return 0;
}