Archived

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

BlueChip

c++ question - double class inclusion

Recommended Posts

hi folks.. I would like this: in A.h
class A
{
 classB* b
}
  
in B.h
class B
{
 classA* a
}
  
of course,I can't put in A.h an Include "B.h" and in B.h an Include "A.h".... I could do this: in A.h
#include "B.h"

class A
{
 classB* b
}
  
in B.h
class A
class B
{
 classA* a
}
#include "A.h"
  
but in B.h I couldn't use A method (like a simple new A).. I.E this give me an error because B don't know an A costructor..
class A
class B
{
 classA* a
 void XXX() {a = new ClassA;}
}
#include "A.h"
  
there is a solution? thanks a lot =) [edited by - BlueChip on October 15, 2003 9:39:21 AM]

Share this post


Link to post
Share on other sites
Make sure you use A or B in the implementation files (*.cpp). It is ok to include the other''s h file there.

IE. (lots of stuff left out for simplicity)

// A.h

class B;
class A{
B* m_aB;

void doSomethingWithB();
};
// end A.h


// B.h

class A;
class B{
A* m_anA;

void doSomethingWithA();
};
// end B.h


// A.cpp

#include "A.h"
#include "B.h"

void A::doSomethingWithB(){
m_aB->doSomethingUsefull();
}
// end A.cpp


// B.cpp

#include "A.h"
#include "B.h"

void B::doSomethingWithA(){
m_anA->doSomethingUsefull();
}
// end B.cpp



Hope this helps
Sabbac

Share this post


Link to post
Share on other sites
Err... I can''t exacly remember how this works (been awhile sience I have coded).

There is a "#if_def" (thats probably not exacly right, closing argument is like) command that goes OUTSIDE all the functions. Basicly I would use it in my .h files and what it does is it wont allow that class to be initialized (or whatever team you would like to use) more than once. So basicly you could declare the class a few billion times if you like but it would only load once. So go ahead and use that in your classes and you can #include them inside of each other without worry of having it initilized more than once.

Anyone able to shed some more light on this?
If not I can probably look up more specific details about how I did it.

PS - Keep the ridicuel (sp?) to a minimum if anything I have mentioned above is... stupid.

Share this post


Link to post
Share on other sites