Helper class uses main class' pointer

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

  1. I am trying to compare assignment operator to the main class’ pointer
    while it is performing the task inside helper class’ operator=
    function.
    Please let me know if my code is to be correct design since I put
    helper class inside main class.


    typedef unsigned int size;

    class A {
    private:
    struct B {
    B() {}
    ~B() {}

    B &operator=( const B &r ) {
    if( a == r.a )
    return *this;

    a->m_data = r.a->m_data;
    return *this;
    }

    B &operator=( const size ) {
    return *this;
    }

    operator size() { return 0; }
    operator size() const { return 0; }

    A *a;
    };

    public:
    A( size data ) : m_data( data ) {
    b.a = this;
    }
    ~A() {}

    B &Go() {
    return b;
    }

    private:
    size m_data;
    B b;
    };


    int main () {
    A a( 0x25 ), a2( 0x7C );

    a.Go() = a.Go(); // Skip assignment operator to the same object
    a.Go() = a2.Go();

    return 0;
    }
    Nephi Immortal, Feb 8, 2011
    #1
    1. Advertising

  2. On Feb 8, 7:03 pm, Nephi Immortal <> wrote:
    >         I am trying to compare assignment operator to the main class’ pointer
    > while it is performing the task inside helper class’ operator=
    > function.
    >         Please let me know if my code is to be correct design since I put
    > helper class inside main class.
    >
    > typedef unsigned int size;
    >
    > class A {
    > private:
    >         struct B {
    >                 B() {}
    >                 ~B() {}
    >
    >                 B &operator=( const B &r ) {
    >                         if( a == r.a )
    >                                 return *this;
    >
    >                         a->m_data = r.a->m_data;
    >                         return *this;
    >                 }
    >
    >                 B &operator=( const size ) {
    >                         return *this;
    >                 }
    >
    >                 operator size() { return 0; }
    >                 operator size() const { return 0; }
    >
    >                 A *a;
    >         };
    >
    > public:
    >         A( size data ) : m_data( data ) {
    >                 b.a = this;
    >         }
    >         ~A() {}
    >
    >         B &Go() {
    >                 return b;
    >         }
    >
    > private:
    >         size m_data;
    >         B b;
    >
    > };
    >
    > int main () {
    >         A a( 0x25 ), a2( 0x7C );
    >
    >         a.Go() = a.Go(); // Skip assignment operator to the same object
    >         a.Go() = a2.Go();
    >
    >         return 0;
    >
    > }
    >
    >


    Hi Nephi

    I try to say a few things, I guess may be helpful
    and I hope they help you so:
    1. The standard names for Main class and Helper class
    are actually "Enclosing' and "Nested'. So A is Enclosing
    and B is Nested class. At least these word are used in C++ standard
    draft.
    2. I think the main usage of nested class
    is when you want to enclose some functionality
    in an abstraction, which it is used by the enclosing
    class and it isn't used directly by outsiders.
    For example in Pimpl idiom or Handle/Body idiom
    the body is a good candidate for nested classes.
    Of course it is a general thing and there are exceptions.
    In your code, I see no benefit come form nested class
    you can decouple A and B and put them in 2 separate
    classes. In that case you have to declare B a friend of A:
    // a.h
    struct B {
    B &operator=( const B &r );
    // ...
    };

    class A {
    friend class B;
    // ...
    };

    Also, you have to define assignment operator
    of B in a .cpp file:


    // a.cpp
    #include "a.h"

    B &B::eek:perator=( const B &r ) {
    if( a == r.a )
    return *this;
    a->m_data = r.a->m_data;
    return *this;
    }

    HTH
    -- Saeed Amrollahi
    Saeed Amrollahi, Feb 9, 2011
    #2
    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. John M
    Replies:
    1
    Views:
    1,115
    Kumar Reddi
    May 29, 2005
  2. Hal Styli
    Replies:
    14
    Views:
    1,615
    Old Wolf
    Jan 20, 2004
  3. gamehack

    Main loop helper functions

    gamehack, Jan 28, 2006, in forum: C Programming
    Replies:
    6
    Views:
    403
    Vladimir S. Oka
    Jan 29, 2006
  4. Crocodile

    Convenience class vs helper class

    Crocodile, Sep 22, 2008, in forum: Java
    Replies:
    0
    Views:
    436
    Crocodile
    Sep 22, 2008
  5. kevin
    Replies:
    1
    Views:
    360
    Kairi Zikpin
    Jul 21, 2006
Loading...

Share This Page