Solved

Dependencies between classes

Posted on 2000-02-18
5
203 Views
Last Modified: 2010-04-02
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
Comment
Question by:rzvika3
  • 2
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
JMu earned 40 total points
ID: 2537795
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
 
LVL 5

Expert Comment

by:Wyn
ID: 2537863
Using forward del.
0
 

Author Comment

by:rzvika3
ID: 2537868
Wyn, What do you mean by forward?
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2537890
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
 

Author Comment

by:rzvika3
ID: 2537897
Thank you very much
both!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now