• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

Dependencies between classes

Hi!
I have a class A and class B.
Class A has an array of class B.
Class B has data member of type A.
What should i include (#include) in each of the classes?
0
rzvika3
Asked:
rzvika3
  • 2
  • 2
1 Solution
 
JMuCommented:
You can't do that because of recursion.

Instead declare array of class B in class A to array (or list) of pointers of class B. Or declare class A as a pointer in class B. Choose the one which is more natural.

The class where you use a pointer doesn't require class definition first, but you have to tell compiler that it's available.

// B.h

class A;  // this class exists

class B
{
  private:
    A* a;
};

--------

// A.h

#include "B.h"

class A
{
  private:
    Array<B> b;
};

I used dynamic template based array approach here.

---------

// A.cpp

#include "A.h"

// code here

---------

// B.cpp

#include "B.h"
#include "A.h"  // we use this here

// code here

JMu
0
 
WynCommented:
Using forward del.
0
 
rzvika3Author Commented:
Wyn, What do you mean by forward?
0
 
WynCommented:
o.sorry:)
Just mean

class a;

now you can use  a in b;
 


and declare a and implement it later .

As JMu told.

//it's so-called forward declaration,
:)

Sorry again:)

Best Regards
Wyn
0
 
rzvika3Author Commented:
Thank you very much
both!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now