a class inside a class

Started by
12 comments, last by alnite 21 years, 2 months ago
class A { class B { }; }; What does that mean? class B belongs to class A. Can you please provide a practical example of this (game-development wise)? I think I understand the concept, but I still don''t understand why as opposed to: class B { }; class A { B something; }; return 0;
Advertisement
You can have your shoes inside your house or outside... so what''s the difference?
"after many years of singularity, i'm still searching on the event horizon"
well, if you do this

class a
{
class b;
};

you hide class b inside class a, class b is within the scope of class a only so if you want to instantiate a class b, you should
a::b

wheras the other one you are freely to use class b,



http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
quote:Original post by alnite
Can you please provide a practical example of this (game-development wise)?


Sure.

   	/** Class representing merely an image file	*/	class CImage	{	protected:		unsigned char *m_pData;		unsigned int m_uiWidth;		unsigned int m_uiHeight;		unsigned int m_uiBPP;	public:		/** Structure representing a 2-D rectangle		*/		struct SRect		{			int iLeft, iTop, iRight, iBottom;			SRect(int l, int t, int r, int b)			{				iLeft = l;				iTop = t;				iRight = r;				iBottom = b;			}			SRect &operator = (const SRect &rkRect)			{				iLeft = rkRect.iLeft;				iTop = rkRect.iTop;				iRight = rkRect.iRight;				iBottom = rkRect.iBottom;				return *this;			}		};	public:		/** Constructor(s)		*/		CImage() 			: m_pData(0)		{ }		/** Default destructor		*/		~CImage() 		{ 			if (m_pData) 			{				delete [] m_pData; 				m_pData = 0;			}		};            /** and bleh, it goes on. */      





[edited by - masterg on January 28, 2003 7:43:55 PM]
masterghttp:/masterg.andyc.org
You would use this for an object that ownes somthing(has a), not deriving from something(is a).

IE. Class AIBot has a gun, so you could do

Class AIBot
{
private:
class gun;
};
CheersChris
quote:Original post by alnite
What does that mean?

Well, it doesn''t mean that there is any structural relationship between instances of class A and class B. In C++, a class serves two purposes. The main purpose is to specify the data and behaviour for objects of that class. The secondary purpose is to effectively serve as a namespace (which, IMO, is stupid). In your example code, class B lives within the namespace scope of class A, but there is no relationship between class A and class B objects.
quote:
Can you please provide a practical example of this (game-development wise)?

It''s actually quite rare to find a practical use. I''ve used it in the past, but I can''t remember why (I think it was something to do with subclassing from a common interface and returning objects where the outside world knew about the interface but not the concrete class). These days, I''d be more inclined to let the outside world see class B, on a basis of trust. i.e. I trust my fellow developers to not use classes in a context that doesn''t make sense.

quote:Original post by chollida1
You would use this for an object that ownes somthing(has a)

It doesn''t express *any* structural relationship between objects.
Thanks for the informative replies people. This, however, raises another question, what is going to happen if I inherit a class C from class A

class C : public A
{
};

Will C inherit B also, considering B is protected/public? Am I allowed to add class members of class B (just like inheritance) within the namespace of C? If I am allowed to do so, now that's something different!

return 0;

[edited by - alnite on January 28, 2003 1:48:38 PM]
quote:Original post by alnite
Thanks for the informative replies people. This, however, raises another question, what is going to happen if I inherit a class C from class A

class C : public A
{
};


Do like this:

  class A{public: // or protected.   class B   {   };};class C : public A{public:   class D : public A::B   {      // Overloads for B.   };   // Overloads for A.};  



Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
quote:Original post by alnite
Thanks for the informative replies people. This, however, raises another question, what is going to happen if I inherit a class C from class A

The same as would happen if B didn't exist.
quote:
Will C inherit B also, considering B is protected/public?

No. There is *no* structural relationship between A and B for C to inherit. C will inherit A, and that's all.

Edit: hold on, you didn't mean that, did you? You can do this:


  class A{public:	class B	{	};};class C : public A{};int main(){	A* a = new C;	A::B* b = new C::B;}  


IOW, B will also exist in C's namespace.

[edited by - SabreMan on January 28, 2003 2:27:20 PM]
And when you define a class inside a class, where do all the things inside the class go? Outside with a scope operator or inside?


tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)

This topic is closed to new replies.

Advertisement