Solved

Dependencies between classes

Posted on 2000-02-18
5
208 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 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