Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Circular Definitions

Posted on 2000-04-12
2
Medium Priority
?
290 Views
Last Modified: 2013-11-20
I have a number of header files that are dependent on each other. For example, the defintion of object A contains a reference to the defintion of object B. And the defintion of object B contains a reference to the defintion of object A. Object A and B are in different files. I have tried to solve the problem by doing the following in the first file:

class B;
class A
{
B objectB;
}

And doing the following in the other file:
class A;
class B
{
A objectA;
}

But the problem is still not solved!!
What am I suppose to do to solve this problem?

Thank you.
0
Comment
Question by:Zainal062797
2 Comments
 
LVL 1

Accepted Solution

by:
MT_MU earned 150 total points
ID: 2709239
I'm not sure why, but if you change to ptr reference it will work.

class B;
class A
{
B* objectB;
}

And doing the following in the other file:
class A;
class B
{
A* objectA;
}

0
 
LVL 1

Expert Comment

by:tao_shaobin
ID: 2709405
The reasons why it works after we change them to pointers are:

B* objectB; is only a declaration.  It won't call anything, including construtors of B.  But B objectB; may call the construtor which may initialize some variables.  

In this case, it is impossible to declare circular declarations if both classes need to initialize some variables in their construtors.  Therefore, we need to prevent this to happen.  If not of all of them need initilization, then you can declare like this(suppose A needs not to initialize)

a.h

#include "B.h"  // only way
class A
{
  B  bobject;
  ....
{

b.h

class A;
class B
{
  A aObject;
  ....
}

Then, your program should work.

Help this helps,

tao_shaobin
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.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

886 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