Nested classes within other classes

Discussion in 'C++' started by Tony Johansson, Dec 14, 2004.

  1. Hello!!

    All my exemples is from a book so don't bother complaining that the struct
    should have been a class instead.

    Below I have two example it says START EXAMPLE 1 and futher down you have
    END EXAMPLE 1 and after END EXAMPLE 1 you find START EXAMPLE 2 and futher
    down you have END EXAMPLE 2.
    In example 1 you have an abstract base class defined as a struct called
    IMotion and
    a class definition called CSpaceship with the class XMotion class nested
    inside this CSpaceship class.

    In example 2 you still have an abstract base class defined as a struct
    called IMotion and the class XMotion is now not nested but it's defined
    lika a normal data member inside class CSpaceship. The class definition
    XMotion is now separete.

    Now to my question what is the difference functional between example 1 and
    example 2. is it the same thing perhaps?

    //Tony


    //START EXAMPLE 1
    //*****************
    struct IMotion
    {
    virtual void Fly() = 0;
    virtual int& GetPosition() = 0;
    };

    class CSpaceship
    {
    protected:
    int m_nPosition;
    int m_nAcceleration;
    int m_nColor;
    public:
    CSpaceship()
    {m_nPosition = m_nAcceleration = m_nColor = 0;}

    class XMotion : public IMotion
    {
    public:
    XMotion(){}
    virtual void Fly();
    virtual int& GetPosition();
    }m_xMotion;

    friend class XMotion;
    };
    //END EXAMPLE 1
    //***************


    //START EXAMPLE 2
    //*****************
    struct IMotion
    {
    virtual void Fly() = 0;
    virtual int& GetPosition() = 0;
    };

    class XMotion : public IMotion
    {
    public:
    XMotion(){}
    virtual void Fly();
    virtual int& GetPosition();
    };

    class CSpaceship
    {
    protected:
    int m_nPosition;
    int m_nAcceleration;
    int m_nColor;
    public:
    CSpaceship()
    {m_nPosition = m_nAcceleration = m_nColor = 0;}

    XMotion m_xMotion;
    };
    //END EXAMPLE 2
    //***************
     
    Tony Johansson, Dec 14, 2004
    #1
    1. Advertising

  2. In terms of functionality there's no difference between example 1 and
    2, they differ in the logical design:
    >From example 1 I read that CSpaceship::XMotion is specific to

    CSpaceship. I hardly would use to move
    an Asteroid around. This example also implies that XMotion could easily
    be friend of CSpaceship if needed so.

    In example 2 the designer tells me that XMotion can be used for a whole
    group of classes
    (tough the criteria are not documented).

    regards,
    Stephan Brönnimann

    http://www.osb-systems.com
    Open source rating and billing engine for
    communication networks.
     
    =?iso-8859-1?q?Stephan_Br=F6nnimann?=, Dec 14, 2004
    #2
    1. Advertising

  3. Tony Johansson

    msalters Guest

    Tony Johansson wrote:
    > Hello!


    > In example 1 you have an abstract base class defined as a struct

    called
    > IMotion and a class definition called CSpaceship with the class
    > XMotion class nested inside this CSpaceship class.


    Yes. In addition, there is _also_ one member CSpaceship::m_xMotion
    which has type CSpaceship::XMotion

    > In example 2 you still have an abstract base class defined as a

    struct
    > called IMotion and the class XMotion is now not nested but it's

    defined
    > lika a normal data member inside class CSpaceship. The class

    definition
    > XMotion is now separete.


    No. XMotion is declared and defined outside CSpaceship. However, the
    data member CSpaceship::m_xMotion is still defined inside CSpaceship.


    > Now to my question what is the difference functional between example

    1 and
    > example 2. is it the same thing perhaps?


    In example 1, the type CSpaceship::XMotion is a private member of
    CSpaceship whereas in example 2, ::XMotion is globally visible.
    That means that in example 1, it's a whole lot easier to find all
    the users of the type. Private members are used only by the class.

    Regards,
    Michiel Salters

    > //START EXAMPLE 1
    > struct IMotion
    > {
    > virtual void Fly() = 0;
    > };
    >
    > class CSpaceship
    > {
    > class XMotion : public IMotion
    > {
    > public:
    > XMotion(){}
    > virtual void Fly();
    > }m_xMotion;
    >
    > friend class XMotion;
    > };
    > //END EXAMPLE 1
    >
    > //START EXAMPLE 2
    > struct IMotion
    > {
    > virtual void Fly() = 0;
    > };
    >
    > class XMotion : public IMotion
    > {
    > public:
    > XMotion(){}
    > virtual void Fly();
    > };
    >
    > class CSpaceship
    > {
    > XMotion m_xMotion;
    > };
    > //END EXAMPLE 2
     
    msalters, Dec 14, 2004
    #3
    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. Razvan
    Replies:
    5
    Views:
    11,364
    Dale King
    Jul 27, 2004
  2. kelvSYC
    Replies:
    2
    Views:
    609
    Chris Smith
    Aug 18, 2004
  3. Replies:
    7
    Views:
    579
    Alex Hunsley
    Aug 12, 2006
  4. Chad E. Dollins
    Replies:
    3
    Views:
    680
    Kai-Uwe Bux
    Nov 8, 2005
  5. Ultrus
    Replies:
    3
    Views:
    411
    Stefan Behnel
    Jul 9, 2007
Loading...

Share This Page