Bi-directional communication in nested classes

Discussion in 'C++' started by Nephi Immortal, Feb 2, 2011.

  1. 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;

    }
     
    Nephi Immortal, Feb 2, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daragoth
    Replies:
    4
    Views:
    620
    Just an Illusion
    Jun 30, 2004
  2. Replies:
    2
    Views:
    498
  3. Abraham Khalil
    Replies:
    1
    Views:
    526
    Knute Johnson
    Jan 26, 2004
  4. Kathryn Bean
    Replies:
    2
    Views:
    10,196
    Alex Kizub
    Oct 13, 2004
  5. Matt
    Replies:
    1
    Views:
    3,545
    SpaceGirl
    Mar 6, 2004
Loading...

Share This Page