Solved

Circular Definitions

Posted on 2000-04-12
2
274 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
[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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ORA-01403: no data found 43 116
How to determine file rename from EventLog ? 2 62
sum28 challenge 31 117
withoutTen challenge 14 142
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

710 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