Link to home
Start Free TrialLog in
Avatar of shadow66
shadow66

asked on

Connecting classes with pointers

I want two classes to hold a pointer to each other.

C++Builder stops my compile with "files nested too deep" when I try this:

File: test.cpp
#include "a.h"
#include "b.h"

int main(){A a; B b; a.link(&b); b.link(&a); return 0;}

File: a.h
#pragma hdrstop
#include "b.h"
#ifndef A_H
#define A_H
Class A {
  public:
     link(B* b_addr){b_ptr = b_addr;}
  private:
    B* b_ptr;
}
#endif

File: b.h
#pragma hdrstop
#include "a.h"
#ifndef B_H
#define B_H
Class B {
  public:
     link(A* a_addr){a_ptr = a_addr;}
  private:
    A* a_ptr;
}
#endif

I don't want to have to create a superclass over these classes (and thus each class would hold a reference to this superclass and use polymorphic calls.) How can I avoid the recursive header file loading?
}
Avatar of shadow66
shadow66

ASKER

Edited text of question.
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
include your a.h in your b.h and then you only need to #include your b.h in your main

or if you have a b.cpp, # include a.h and b.h in b.cpp and #include b.cpp in your main

Thanks for the quick response (for so few points...). I'm trying to learn C++ on my own--through books, CDs, etc. Any book recommendations?

Thanks again.
Sorry, I haven't read a C++ study book for a long time. I have been reading some references and documentation.