Nested classes within other classes

T

Tony Johansson

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
//***************
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

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
(e-mail address removed)
http://www.osb-systems.com
Open source rating and billing engine for
communication networks.
 
M

msalters

Tony said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top