473,414 Members | 1,776 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,414 software developers and data experts.

best C++ solution? (fwd decl, circular ref)

293 100+
How can I call a.Print() from a.b.Recurse()? Adding a pointer in b.Recurse(A *ptr) causes undeclared errors, and adding a forward declaration was an invalid type of forward declaration. I copied an example using header files, and the forward declaration problem still existed. I used both Cygwin and Mingw compilers.

Obviously, this is a simplified example. One solution is to nest class B within class A. However, in my actual code, class A has a vector of class B. I just tested it, that would work fine, but is there a better way to solve this?

Example code
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3.  
  4. class B
  5. {
  6. public:
  7.   void Recurse()
  8.   {
  9.     this->Print(); // want a.Print(), not b.Print()
  10.   }
  11.  
  12.   void Print()
  13.   {
  14.     cout << "B" << endl;
  15.   }
  16. };
  17.  
  18. class A
  19. {
  20.   public:
  21.   B b;
  22.  
  23.   void Recurse()
  24.   {
  25.     b.Recurse();
  26.   }
  27.  
  28.   void Print()
  29.   {
  30.     cout << "A" << endl;
  31.   }
  32. };
  33.  
  34. int main()
  35. {
  36.     A a = A();
  37.     a.Recurse();
  38.  
  39.     system("PAUSE");
  40.     return 0;
  41. }
Jun 20 '06 #1
2 2370
Banfa
9,065 Expert Mod 8TB
The mistake you are making is putting your class code into the class definition. If you define the class separately to where you put the class code then you will be able to successfully make a forward reference to class A without causing errors

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3.  
  4. class A;
  5.  
  6. class B
  7. {
  8. public:
  9.   void Recurse(A &a);
  10.   void Print();
  11. };
  12.  
  13. class A
  14. {
  15.   public:
  16.   B b;
  17.  
  18.   void Recurse();
  19.   void Print();
  20. };
  21.  
  22. void B::Recurse(A &a);
  23. {
  24.   a.Print(); // want a.Print(), not b.Print()
  25. }
  26.  
  27. void B::Print()
  28. {
  29.   cout << "B" << endl;
  30. }
  31.  
  32. void A::Recurse()
  33. {
  34.   b.Recurse(*this);
  35. }
  36.  
  37. void A::Print()
  38. {
  39.   cout << "A" << endl;
  40. }
  41.  
  42. int main()
  43. {
  44.     A a = A();
  45.     a.Recurse();
  46.  
  47.     system("PAUSE");
  48.     return 0;
  49. }
  50.  
I haven't tested/compiled this code, but the principle is sound. This works because during the class definition all B needs to know is that class exists, it does not need to know what members it has. It only needs to know what members it has during the implementation of it's methods which it does once the code is re-written like this.
Jun 21 '06 #2
D_C
293 100+
Thank you.
Jun 22 '06 #3

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

Similar topics

8
by: Ralph Freshour | last post by:
Is it possible to inhibit the browser Back/Fwd buttons via PHP? Thanks...
4
by: ro86 | last post by:
Hello everyone! I am a newbie to C++ (~1 Week experience) and I have a few months of experience with object-oriented languages (Objective-C). I am currently working just for fun on a particle...
8
by: Eric Eggermann | last post by:
I'm having a problem with really large file sizes when serializing the classes that describe my little document. There are some circular references which result in the same object getting written...
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: _DD | last post by:
I believe Balena's Best Practices book suggests grouping quite a few classes into each namespace. I don't remember a number, but this has me curious about how other programmers handle this. If...
1
by: Will | last post by:
Our shop has 6 .Net developers, most of our work to date has been one developer on a project at a time, occasionally 2 for brief periods of time. We have souce control (SourceGear) and love what...
1
by: jake77.lucas | last post by:
I'm using 2005.Net C++ compiler. I have a base class in namespace A (call it Class1). I derive another class from it, (call it Class2) defined in namespace B. This derived class contains as a...
1
by: whizkid | last post by:
Hi... Looking for a solution to a problem that I am facing... Basically I have a table schema defined as follows. Table X( br char(2), Dealno char(8) SeqNo char(4)
3
by: =?Utf-8?B?UGF1bCBIYWxl?= | last post by:
Moving all User Controls to a single directory has solved my problem - Thanks Eliyahu. That said, I still got one Circular ref error yesterday, rebuilt again and the build was fine? Far far...
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
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
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...
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...
0
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...

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.