473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Circular headerfile inclusion problem

110 100+
hi all,

I have 3 classes. class A, class B, and class C, class D, defined in 4 files A.cpp, B.cpp, C.cpp, D.cpp
inheritence hierarchy is as follows.
A--> B --> C
|
|
V
D
i.e, A is inherited by B which is inherited by C and D.
then, A and B has a virtual function test() overridden in C and D.
and I need to use dynamic binding as follows..
Expand|Select|Wrap|Line Numbers
  1. //this would be a member function of A
  2. bool A::SomeFunc()
  3. {
  4. A * var;
  5. if(some condition)
  6.     var = new C();
  7. else
  8.     var = new D();
  9. ..
  10. ..
  11. ..
  12. return TRUE;
  13. }
  14.  
so problem is..
A needs C and D.
C and D needs B.
and B needs A.
so there is a circular inclusion.
becuase of which base classes inherited are not recognised.
can anybody suggest a solution? is this a common problem?

waiting for your reply,
Xoinki
Mar 19 '08 #1
1 2145
Banfa
9,065 Expert Mod 8TB
This should not be a problem as long as bool A::SomeFunc() is defined in a CPP file and not a header file.

It is just a matter of including the headers in the right order at the top of the A.CPP file, probably

Expand|Select|Wrap|Line Numbers
  1. #include "A.h"
  2. #include "B.h"
  3. #include "C.h"
  4. #include "D.h"
  5.  
This is more of a problem if there are 2 classes X and Y each of which has methods that take a pointer or reference to the other class. In this case you need to know and X in Y.H and Y in X.H which does create a header file interdependency.

The way round this is a forward declaration, this looks something like
Expand|Select|Wrap|Line Numbers
  1. class X;
  2.  
This tells the compiler "at some point in the future I am going to declare a class named X" and provides the compiler with enough information to create pointers and references to the class (but not to access the class members).
Mar 19 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Henry Miller | last post by:
I have the following code (much simplified for this post). Note that SessionKey uses DataAccess, and DataAccess requires SessionKey in it's constructor. Public Class SessionKey Public...
16
by: Kiuhnm | last post by:
Is there an elegant way to deal with semi-circular definitions? Semi-circular definition: A { B }; B { *A }; Circular reference: A { *B }; B { *A }; The problems arise when there are more...
6
by: T Koster | last post by:
After a few years of programming C, I had come to believe that I finally knew how to correctly organise my structure definitions in header files for mutually dependent structures, but I find myself...
6
by: Stephen Robertson | last post by:
We are currently in a dead end with a circular reference issue using vb.net, and are hoping someone might help us resolve it. Idea... We have frmmain calling frmperson (dim f as new frmperson)...
3
by: Solution Seeker | last post by:
Hi All, I am here with a Query and need a Solution for it. The Query is as Follows, We have 3 Projects in a Solution - Say UI, CMN and PRD First One Deals with UI Forms Second One Deals...
7
by: barias | last post by:
Although circular dependencies are something developers should normally avoid, unfortunately they are very easy to create accidentally between classes in a VS project (i.e. circular compile-time...
9
by: rammel | last post by:
class1.h ----------- #ifndef CLASS1_H_ #define CLASS1_H_ #include "class2.h" // inclusion class class2; // and forward class class1 { private:
6
by: Juha Nieminen | last post by:
Multiple inclusion of the same header file can cause the compilation to fail because of multiple definitions of the same type. That's why it's standard practice to write all headers like this: ...
3
by: joshc | last post by:
I've found some old posts on this issue but the answer was a bit vague to me. The problem I am trying to solve is the following: /***** a.h ***********/ #include "b.h" /* Forward declaration...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.