Solved

Circular Definitions

Posted on 2000-04-12
2
268 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 50 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

679 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