views:

629

answers:

4

So I have a class A, where I want to call some class B functions. So I include "b.h". But, in class B, I want to call a class A function. If I include "a.h", it ends up in an infinite loop, right? What can I do about it?

+20  A: 

Put only member function declarations in header (.h) files, and put member function definitions in implementation (.cpp) files. Then your header files do not need to include each other, and you can include both headers in either implementation file.

For cases when you need to reference the other class in member signatures as well, you can use a forward declaration:

class A;

This lets you use pointer and reference types (A* and A&), though not A itself. It also doesn't let you call members.

Example:

// a.h
struct B; // forward declaration

struct A {
   void foo(B* b); // pointers and references to forward-declared classes are ok
};


// b.h
struct A; // forward declaration

struct B {
   void bar(A& a); // pointers and references to forward-declared classes are ok
};


// a.cpp
#include "a.h"
#include "b.h"

void A::foo(B* b) {
   b->bar(*this); // full declaration of B visible, ok to call members now
}


// b.cpp
#include "a.h"
#include "b.h"

void B::bar(A& a) {
   a.foo(this); // full declaration of A visible, ok to call members now
}
Pavel Minaev
+3  A: 

You can also use forward declarations to get around the issue.

rerun
+5  A: 

Each class (A and B) should have a header file and an implementation file.

Each header file (e.g. A.h) should not include the other header file (e.g. B.h) but may include a forward reference to the other class (e.g. a statement like class B;), and may then use pointers and/or references to the other class in its declaration (e.g. class A may contain a B* as a data member and/or as a method parameter).

Each CPP file (e.g. A.cpp) may include more than one header file (e.g. A.h and B.h). It's recommended that each CPP file should include its own header file first (e.g. A.cpp should include A.h and then B.h, whereas B.cpp should include B.h and then A.h).

Each header file should contain only the declaration, and not the definition of the class: for example it will list the signatures of the class' methods, but not the method bodies/implementations (the method bodies/implementations will be in the .cpp file, not in the header file). Because the header files don't contain implemention details, they therefore don't depend on (don't need to see) details of other classes; at most they need to know that, for example, B is the name of a class: which it can get from a forward declaratin, instead of by including a header file in another header file.

ChrisW
A: 

These answers do not answer the question. Try putting #ifndef #define and #endif around your .h files.

James G.