Help with C++ classes

Ok, this might be a bit hard to understand, but this is what I want to do:

Lets say we have class A and class B:
1
2
3
4
5
class A {
public:
    A();
    ~A();
}

1
2
3
4
5
class B {
public:
    B();
    ~B();
{


So how do I make class A include class B and at the same time
make class B include class A?

Any help is appreciated!

P.S: When I say include I mean as in "#include"s
Last edited on
You can't. If one of the headers only have pointers or references to the other class and never actually access the object it's enough to add a forward declaration.
1
2
// Forward declaration of class A.
class A;
Hi! Thanks for the reply! I'm just beginning with C++ so could you explain a bit more? I'm sorry if this is to much to ask, but I am really eager to learn how to do what you just did!
@Peter87
A.h
1
2
3
4
5
6
7
8
9
#include "B.h"

class A 
{
public:
	void foo();
private:
	B a;
};


B.h
1
2
3
4
5
6
7
class A;

class B 
{
public:
	void bar(A& a);
};


In the above example A has a data member of type B so it necessary to include B.h. The only use of A in B is a reference so we don't have to include A.h. Including A.h from B.h wouldn't even work because they include each other. We still need to tell the compiler that A is a class so for that reason we put a forward declaration of A (line 1) in B.h. In B.cpp you will probably have to include A.h, otherwise you will not be able to access the A object (call member functions, etc.) from the A reference.
Last edited on
I see...
Thank you sir! I fully understand now!
Topic archived. No new replies allowed.