Solved

Circular Definitions

Posted on 2000-04-12
2
235 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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
paragon account problem 9 96
Strange date stored 22 99
bigDiff challenge 17 74
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 81
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now