473,405 Members | 2,310 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,405 software developers and data experts.

Problem with cyclic dependency

Hi,
I am having some difficulties with cyclic dependency between two classes. Situation is something like following -

Expand|Select|Wrap|Line Numbers
  1. ///A.h
  2. #include "B.h"
  3.  
  4. class A {
  5. {
  6.    int X;
  7. public:
  8.    P() { Q(); }
  9.    Q() {
  10.       R();
  11.       bb = new B();
  12.       bb.D( this ); )
  13.    R() { uses X )
  14.  
  15. };
  16.  
  17. ///B.h
  18. class B: public someclass {
  19. public:
  20.    D(A& a) {
  21.       a.Q();        //end condition for recursion depends on X 
  22.       a.R();
  23.  
  24. };
SOME BACKGROUND-
I couldn't create D() in A as D() access private data in B. But D() calls
both Q() and R() and value of X has to be same everywhere. i.e. R()
called from D access the same X value that was used by class A. That's why I am passing this pointer of class A to B. There will be just one instance of A and B. But there isn't just one class B, there are lots of classes like B derived from a parent class. I am actually creating objects for these classes, kind of linked list. A has functions for creating a list, but it may be nested that's why there is a recursion.

PROBLEM-
Forward declaration of A in B doesn't work and I can't include header of A to B as header of B is included in A. I am using kdevelop on suse.
How do I solve this thing? Sorry for such a long description.
Any help would be very appreciated. Thanks.
Apr 5 '08 #1
3 1725
Savage
1,764 Expert 1GB
You cannot use forward declaration because you need to access class methods, and by giving it only a forward declaration there is no way for compiler to know which are the methods and members of the forward declared class.
Instead try declaring a interface for class A in B.h which will contain Q() and R(). That interface will be passed as reference to D in class B.In A.h you will derive class A from this interface and trough polymorphism you should be able to get this to work.

Savage
Apr 5 '08 #2
You cannot use forward declaration because you need to access class methods, and by giving it only a forward declaration there is no way for compiler to know which are the methods and members of the forward declared class.
Instead try declaring a interface for class A in B.h which will contain Q() and R(). That interface will be passed as reference to D in class B.In A.h you will derive class A from this interface and trough polymorphism you should be able to get this to work.

Savage
Thanks a lot for the reply, I will just try that.
Apr 5 '08 #3
Beautiful, works like a charm.
Thanks
Apr 6 '08 #4

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

Similar topics

3
by: Dennis Lerche | last post by:
Hi I have a problem regarding cyclic dependency, yeahh I know bad design. But right at this moment I can't see how it should be redesigned to avoid this. The problem is that I just can't get it...
3
by: Flare | last post by:
Hi I have this C# code --------------------------------- class BaseUnit; .... class ResizeBorder : Shape .... class Shape : BaseUnit {
4
by: Alex Sedow | last post by:
For example, System.dll contains assembly reference to System.XML.dll, System.XML.dll refers to System.dll. Some another open projects contain cyclic assembly dependencies too. How C# compiler...
3
by: Pohihihi | last post by:
This is a report from VSS newsgroup in hope of getting some more suggestions. Thanks, Po ------------------------------------------- I guess I am not getting the whole picture here. How does...
3
by: fc2004 | last post by:
Hi, Is there any tools that could report where cyclic header dependency happens? this would be useful when working with a large project where tens or hundreds of headers files may form complex...
4
by: sakis.panou | last post by:
Hi all, Can anyone explain to me why the copy constructor of the COuterClass is getting called for this one? Let me start by saying I reckon this is seriously bad way of implementing anything of...
10
by: toton | last post by:
Hi I have a class called Session, which stores a vector of Page, like vector<PageAlso each Page need's to know the Session to which it is attached. Thus I have, (the classes has many other...
9
by: Jerome Durand | last post by:
Hello, I'm trying to write something along the following lines but I cannot get this to compile. template <typename derivedstruct Base { typedef typename derived::valueType valueType;...
8
by: Jun | last post by:
Hello everyone, I've two class A and B, in head file : ========================= class A{ // A will use type BPtr
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.