Solved

Dependencies between classes

Posted on 2000-02-18
5
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

726 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