Jump to content
  • Advertisement
Sign in to follow this  
darenking

SOLVED!!!! c++ how do I nest a class?

This topic is 4702 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

Howdy gang. I need to nest a class inside another class. I've seen some examples of it on the interweb but none which address my two main problems. The first is that my code is in separate files (.h and .cpp), whereas most examples don't address this. Second issue, my two classes are huge (my World object and a Loader object that sits along side it but I want to put inside it), so I would prefer to keep the code in two separate files. Any takers? [Edited by - darenking on August 31, 2005 7:36:56 AM]

Share this post


Link to post
Share on other sites
Advertisement
you could do something like this:

EDIT: modified the source files.. check 'em out now

foo.h

#ifndef __foo_h
#define __foo_h

#include <string>
#include <list>

class Foo
{
public:
Foo();
void DoSomething();

private:
class Bar
{
public:
Bar();

void DoSomethingElse();
};


};

#endif




foo.cpp

#include "foo.h"

Foo::Foo()
{
;
}

void Foo::DoSomething()
{
;
}





//bar.cpp

#include "foo.h"

Foo::Bar::Bar()
{
;
}

Foo::Bar::DoSomethingElse()
{
;
}





is that what you wanted?

~guyaton

[Edited by - guyaton on August 30, 2005 11:37:36 AM]

Share this post


Link to post
Share on other sites
I'm confused now. I said don't we need an include for bar.h and you said we do and you've put one in, but there isn't one there and anyway there isn't a bar.h so why did I say it?

I think I shall have a lie down.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Guyaton,

A name with double underscore is reserved for the implementation among other types of names. See 17.4.3.1 for the details.


// ville

Share this post


Link to post
Share on other sites
OK gang, I now have my World object with a nested class called WorldLoader.

In the update method of World, I check to see if new data needs to be loaded (started a new level) and if so, create a new instance of WorldLoader and access its Load method:


//in world.cpp... (simplified, I check for load failure and stuff)

if ( ! m_Valid )
{
m_Valid = true;
WorldLoader worldLoader;
worldLoader.Load();
}



Question is, how does WorldLoader access the data members of World? It needs to load bitmaps into its vectors, for example.

Here are some of the variables, objects and Allegro bitmaps that will need to be modified. They are all private, would rather not make them public as I don't want the object that creates World to be able to access them. These are part of World. WorldLoader is a private member of World too.


private:
int AmountOfHairyScaryMonsters;
Maze m_Maze;
vector<Actor*> m_Actors;
vector<BITMAP*> m_Sprites;
Camera m_Camera[9];
vector< vector<int> > m_Cycles;

class WorldLoader
{
public:
WorldLoader();
~WorldLoader();
void Load();
}




Share this post


Link to post
Share on other sites
Quote:
Original post by darenking

Question is, how does WorldLoader access the data members of World? It needs to load bitmaps into its vectors, for example.


...nested classes don't implicitely have access to their parent classes.

To be able to access the parent, it needs a data member that points to the parent (ie, World* m_Parent), and then the parent would need to set that... somehow. For example, WorldLoader::WorldLoader(World*) would set it, and then the world would do WorldLoader worldLoader(this) to make the loader, and then it could call worldLoader.Load();.

However, this will not provide access to the private members. For that, World must have "friend WorldLoader;" in the declaration. Then loader can access private members through the m_Parent pointer. For example, to access the Sprites, it can use m_Parent->m_Sprites.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!