Solved

Dependencies between classes

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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.

775 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