Jump to content
  • Advertisement

Archived

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

alnite

a class inside a class

This topic is 5655 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
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
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
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
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!