Sign in to follow this  

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

This topic is 4491 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
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
Quote:
Original post by darenking
So there is no way of doing it whereby it would be able to access them with m_Parent.m_Sprites rather than m_Parent->m_Sprites ?


Sure there is, use a reference.

class World {
... m_Sprites;
public: //for example main() only
class Loader;
private:
friend class Loader;
};

class World::Loader {
World & m_Parent;
public:
Loader( World & Parent )
: m_Parent( Parent )
{
}
void Example () {
m_Parent.m_Sprites = ...;
... & Sprites = m_Parent.m_Sprites;
Sprites = ...;
}
};

int main () {
World MyLittlePlanet;
World::Loader Loader( MyLittlePlanet );
Loader.Example();
}



Share this post


Link to post
Share on other sites
Quote:
Original post by skjinedmjeet
What would the advantage of a nested class over a non-nested class be anyway?


Visual grouping and access control ( you can make a nested class private, and thus unusable outside of the class and it's friends itself ). Minor advantages, but on the flip side I can't think of any disadvantages to the practice.

Share this post


Link to post
Share on other sites

This topic is 4491 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this