How do you make 2 classes aware of each other?

Started by
7 comments, last by EGD Eric 20 years, 1 month ago
In C++, if I want to have an instance of class A in class B, and an instance of class B in class A, I can''t do it. Because I have to get class A to include class B''s header file, then class B has to include class A''s header file, and for some stupid reason, C++ doesn''t let you do that. You get all kinds of weird syntax errors if 2 header files are including each other. This seems to me to be a big flaw in the C++ language.
Advertisement
First i am assumeing both of your header files are usieng the...

#ifndef MYHEADER_H
#define MYHEADER_H

#endif

means of checking.

Secondly, If i read your question right... you are trying to do this right?

psuedo code:

Class A {
private:
B myLocalB;
}
Class B {
private:
A myLocalA;
}

C++ does one pass at compile time, so if class A includes class B, it goes and looks at the code for class B, and sees Class B has a class A in it, so how is c++ going to know what a class A is, if it hasnt compiled it yet. I think this is what is happening? not sure.

Actually, let me just say this? WHY are you doing this in the first place, what are you trying to accomplish?

//A.h
class B;
class A
{
B* b;
};

//A.cpp
#include "B.h"
...

//B.h
class A;
class B
{
A* a;
};

//B.cpp
#include "A.h"
Organizing Code Files in C and C++
quote:Original post by EGD Eric
In C++, if I want to have an instance of class A in class B, and an instance of class B in class A, I can''t do it. Because I have to get class A to include class B''s header file, then class B has to include class A''s header file, and for some stupid reason, C++ doesn''t let you do that. You get all kinds of weird syntax errors if 2 header files are including each other. This seems to me to be a big flaw in the C++ language.

It''s not a flaw per se, but a limitation, and not in the C++ language proper, but in the preprocessor, which is a rather primitive tool. Understand that all that the preprocessor does is perform text replacement in various ways and under various conditions; in the case of #include, it takes the contents of another file and pastes it - verbatim - into the current one. If A.h #includes B.h and B.h #includes A.h, without taking any precautions, you will paste into A.h a copy of B.h containing a copy of A.h containing ...

On that topic, I wonder if the next C++ standard will address preprocessor issues and capabilities in any way ...?
Foward declaration is what you want ( an example was given by sjelkjd in a previous post ).
It''s worth pointing out why this doesn''t work, and why it should never work.

my example classes:

class A
{
public:
unsigned int value;
B other;
};

class B
{
public:
unsigned int value;
A other;
};

Ok, problem 1 is that the compiler doesn''t know how much space class A takes up, because it hasn''t seen class B yet. That seems fair enough, however, assuming that the compiler just magically let this through, there is another problem. If, at runtime, you create an instance of either of those objects, how much memory will it take up? I''ll give you a clue; an infinite amount.

A will allocate a B, that B will allocate an A, that A will allocate a B...you can see where this is going

Forward declarations are the way.
And by "forward declarations", I mean "forward declarations and pointers to objects rather than instances", as has been previously pointed to.
quote:Original post by EGD Eric
In C++, if I want to have an instance of class A in class B, and an instance of class B in class A, I can''t do it. Because I have to get class A to include class B''s header file, then class B has to include class A''s header file, and for some stupid reason, C++ doesn''t let you do that.


It''s not stupid at all. If you say "all Xs contain a Y" and "all Ys contain an X" then surely any X or Y would have infinite size, as an X contains a Y, which in turn contains an X, containing a Y, and so on.

So you need to use a pointer or reference to make it clear that 2 items refer to each other, but are not contained by each other.

This is easily done with #includes but, as mentioned by others, forward declarations will be needed.


[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

This topic is closed to new replies.

Advertisement