473,659 Members | 2,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Circular headerfile inclusion problem

110 New Member
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 2163
Banfa
9,065 Recognized Expert Moderator Expert
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
2090
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 IsValidSession as Boolean Sub New(UserName, Password) ' Create session, including calls on DataAccess ' to validate username/password
16
2828
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 semi-circular definitions and
6
2824
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 stumped again with this little love triangle. Here is some background: m_commands.h defines - struct command_callbacks, which contains - a struct conn * reference - struct command, which contains - a struct command_callback reference
6
5067
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) in search (no record) mode. When the search is executed, frmperson calls frmsearchresult (dim f as new frmsearchresult) which is a listing of persons. From frmsearchresults, frmperson is called (dim f as new frmperson) with the resulting...
3
3645
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 with Common Functions that are needed for the Solution
7
13512
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 dependencies). But then I started wondering how "easy" it would be to similarly make a NON-RUNTIME circular dependency between (implicitly linked) DLLs. Indeed authors like John Lakos, who focus on compile/link-time dependencies (not run-time),...
9
1627
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
3904
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: // SomeClass.hh #ifndef SOME_CLASS_HH #define SOME_CLASS_HH class SomeClass {
3
6228
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 */ struct type2; typedef struct type1
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8335
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.