Question about circular reference


As the following four files, the codes will not compile due to header circular reference.

Here is my dilemma. A is a parent with multiple kids B, so A contains B. B sometimes need to retrieve some parameters from A, which I used a pointer inside B. This approach is very common for me to use in C#, but I just cannot figure out how to do it in C++, as A need to have B.h to know B, and B will will need A.h to have pointer A. Is there a syntax to handle it?
Thanks

A.h

#include <vector>
#include "B.h"

using namespace std;

class A
{
public:
A(void);
~A(void);
vector<B> childList;
};

A.cpp

#include "StdAfx.h"
#include "A.h"

A::A(void)
{
}

A::~A(void)
{
}



B.h

#include "A.h"

class B
{
public:
B(void);
~B(void);
A* parent;
};


B.cpp

#include "B.h"

B::B(void)
{
}

B::~B(void)
{
}





You can forward declare classes
eg:

B.h
1
2
3
4
5
6
7
8
9
10
class A; // forward declaration
// #include "A.h" no need here, #include it in B.cpp

class B
{
  public:
    B(void);
    ~B(void);
    A* parent;
};
thanks.
Topic archived. No new replies allowed.