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

Started by
14 comments, last by Erzengeldeslichtes 18 years, 7 months ago
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]
Advertisement
Something like that?

class Foo{   class Bar;};class Foo::Bar{};

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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]
~guyaton
Excellent but don't we need an include for bar.h in bar.cpp? (error in the example?)
yes we do. sorry bout that. fixed now.

~guyaton
~guyaton
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.
I've got it working anyway. You're my hero. Have rated you up to the max.
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
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();	}



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.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?

This topic is closed to new replies.

Advertisement