-
February 6th, 2004, 02:23 PM
#1
cross-referencing #includes
This may sound a n00b question, but say I have two files, a.h and b.h. a.h contains a class, a, and b.h contains a class,b. class a has a member of type b, and vice versa for b. How would i arrange the include statements so that it compiles correctly?? thankyou.
-
February 6th, 2004, 02:34 PM
#2
-
February 6th, 2004, 02:59 PM
#3
You can't do this directly. One solution is to use a third header file called defs.h which has forward declarations for the classes. However that presupposes that you are using pointers to classes.
It *can* also be an indication that your design is bad. In any case, I would look at the design again and ask myself, why I would need to do this.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
February 6th, 2004, 03:08 PM
#4
Put a forward declaration in each file, eg
In a.h
class b; // forward declaration
//note do NOT #include "b.h"
class a
{
...
b m_b;
};
------------------------------
In b.h
#include "a.h"
class b
{
...
a m_a;
};
-------------------------------
In the classes, stick to using pointers, otherwise you may find you run into problems, depending on what you're trying to do with the other class.
You may need to do the forward declaration the other way around, depending on which file your compiler looks at first.
This may be a sign of bad design, but is frequently inevitable. For example you have to do this to implement the 'visitor pattern' - a famous OO design solution.
Last edited by JonnoA; February 6th, 2004 at 03:11 PM.
-
February 6th, 2004, 04:53 PM
#5
Take a look at the following FAQ...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|