Archived

This topic is now archived and is closed to further replies.

alnite

a class inside a class

Recommended Posts

alnite    3436
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;

Share this post


Link to post
Share on other sites
mickey    100
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,



Share this post


Link to post
Share on other sites
masterg    100
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]

Share this post


Link to post
Share on other sites
chollida1    532
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;
};

Share this post


Link to post
Share on other sites
SabreMan    504
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.

Share this post


Link to post
Share on other sites
alnite    3436
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]

Share this post


Link to post
Share on other sites
dalleboy    324
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''

Share this post


Link to post
Share on other sites
SabreMan    504
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]

Share this post


Link to post
Share on other sites
Tazel    122
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)

Share this post


Link to post
Share on other sites
alnite    3436
quote:
Original post by SabreMan
Edit: hold on, you didn''t mean that, did you? You can do this:
Oops, looks like my fingers and my mind spoke different language. This was what I mean: Does B exist in C when C inherit A considering B is protected/public? If so, can I overload B within the namespace of C without declaring a new class (class D, in dalleboy''s example).

  
class A
{
public:
class B
{
int foo;
};
};

class C : public A
{
class B // overloaded B

{
int newfoo;
};
};



return 0;

Share this post


Link to post
Share on other sites
Fruny    1658
Tazel - Theoretically both are possible. In practice, some compilers (e.g. Visual Studio) have trouble with templated classes which have non-inline members (including member classes).

alnite - You''re not overriding (nor even overloading), you''re hiding, just as you would a static member function.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
SabreMan    504
quote:
Original post by SecondBest
As to where or why you use this particular technique; it''s used all over the STL(e.g.list::iterator)

No it isn''t. The technique you are talking about is to code a typedef within the list class, but the iterator class will be defined elsewhere.

Share this post


Link to post
Share on other sites