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

Mutually dependent classes where both have only inline functions?

What is the best way to handle this?
Jun 3 '06 #1
4 3818
* Peter Olcott:
What is the best way to handle this?


The number of tails a dog has, depends on the dog.

But if you can't separate member function implementations from the
classes (first option), and you can't put the two classes in the same
header file (second option), you will probably have to introduce at
least one intermediary class (third option).

E.g. instead of A <-> B, with the third option you'll then have A ->
BAbstract, B -> BAbstract and B -> A, where "->" means "depends on".

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 3 '06 #2
Peter Olcott wrote:
What is the best way to handle this?


Forward declaration and implementing the inline functions after both
class declarations with the inline specifier.

Jun 3 '06 #3

"Markus Schoder" <a3*************@yahoo.de> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
Peter Olcott wrote:
What is the best way to handle this?


Forward declaration and implementing the inline functions after both
class declarations with the inline specifier.


This advice works correctly, although I did not seem to need the forward
declaration. Can you look and see why I did not need the forward declaration,
and thus in what cases I would need it? Thanks

#include <stdio.h>
//
// Example of Complex Mutual Dependency and Forward Declaration.
//
// *** NOTE *** We can not declare class A within class B when class B
// is already declared within class A because this would
// result in infinitely recursive declaration.
//
struct XYZ {
int XXX;
int YYY;
int ZZZ;
XYZ(){};
XYZ(int X, int Y, int Z);
void Out();
void Out(struct ABC& abc);
};
struct ABC {
int AAA;
int BBB;
int CCC;
ABC(int A, int B, int C);
void Out();
struct XYZ xyz;
};

inline XYZ::XYZ(int X, int Y, int Z) {
XXX = X;
YYY = Y;
ZZZ = Z;
}
inline void XYZ::Out() {
printf("XXX(%d) YYY(%d) ZZZ(%d)\n", XXX, YYY, ZZZ);
}
inline void XYZ::Out(struct ABC& abc) {
abc.Out();
}
inline void ABC::Out() {
printf("AAA(%d) BBB(%d) CCC(%d) ", AAA, BBB, CCC);
xyz.Out();
}
inline ABC::ABC(int A, int B, int C) {
AAA = A;
BBB = B;
CCC = C;
xyz.XXX = A + 10;
xyz.YYY = B + 10;
xyz.ZZZ = C + 10;
}
int main( int argc, char *argv[] )
{
ABC abc(1,2,3);
XYZ xyz(24,25,26);
abc.Out();
xyz.Out();
xyz.Out(abc);
}


Jun 3 '06 #4
Peter Olcott wrote:
"Markus Schoder" <a3*************@yahoo.de> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
Peter Olcott wrote:
What is the best way to handle this?
Forward declaration and implementing the inline functions after both
class declarations with the inline specifier.


This advice works correctly, although I did not seem to need the forward
declaration. Can you look and see why I did not need the forward declaration,
and thus in what cases I would need it? Thanks

#include <stdio.h>
//
// Example of Complex Mutual Dependency and Forward Declaration.
//
// *** NOTE *** We can not declare class A within class B when class B
// is already declared within class A because this would
// result in infinitely recursive declaration.
//
struct XYZ {
int XXX;
int YYY;
int ZZZ;
XYZ(){};
XYZ(int X, int Y, int Z);
void Out();
void Out(struct ABC& abc);


Using struct ABC here instead of just ABC makes this a declaration for
struct ABC as well as being a parameter type specification. It is
rarely used in C++ since in case of a typing error you inadvertently
declare a new class name leading to cryptic error messages about an
incomplete type at a later point. It is correct C++ however.
};
struct ABC {
int AAA;
int BBB;
int CCC;
ABC(int A, int B, int C);
void Out();
struct XYZ xyz;
};

inline XYZ::XYZ(int X, int Y, int Z) {
XXX = X;
YYY = Y;
ZZZ = Z;
}
inline void XYZ::Out() {
printf("XXX(%d) YYY(%d) ZZZ(%d)\n", XXX, YYY, ZZZ);
}
inline void XYZ::Out(struct ABC& abc) {
abc.Out();
}
inline void ABC::Out() {
printf("AAA(%d) BBB(%d) CCC(%d) ", AAA, BBB, CCC);
xyz.Out();
}
inline ABC::ABC(int A, int B, int C) {
AAA = A;
BBB = B;
CCC = C;
xyz.XXX = A + 10;
xyz.YYY = B + 10;
xyz.ZZZ = C + 10;
}
int main( int argc, char *argv[] )
{
ABC abc(1,2,3);
XYZ xyz(24,25,26);
abc.Out();
xyz.Out();
xyz.Out(abc);
}


Jun 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Sticks | last post by:
why would i use a class in php, and how do i use a class in php?
4
by: MackS | last post by:
Hi I'm new to Python, I've read the FAQ but still can't get the following simple example working: # file main_mod.py: global_string = 'abc' def main():
5
by: Doug Baroter | last post by:
Hi, DDL: -- create table #task (taskID int identity(1,1) primary key, taskName varchar(25) unique, taskCompleteDate dateTime, taskComplete bit default(0)); /* Business Rules: a) if...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
10
by: Brad Kartchner | last post by:
In a project I am working on, I have found myself with two classes that reference each other. For example: firstclass.h: { class FirstClass { function (SecondClass* Pointer); } }
14
by: pmclinn | last post by:
I've noticed that many programmers use classes to store data about such things like: Class Customers .....Phone ....ID ....Address End Class....
16
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one...
19
by: dl | last post by:
I'll try to clarify the cryptic subject line. Let's say I have a base class 'GeometricObject' with a virtual method 'double distance (const GeometricObject &) const'. Among the derived classes...
3
by: Thomas Pajor | last post by:
Hey everybody, I got into serious trouble with template programming. I have a class which uses three template arguments, say template<typename Atype, typename Btype, typename Ctype> class...
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
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
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...
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,...

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.