Mutually dependent classes where both have only inline functions?

A

Alf P. Steinbach

* 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".
 
M

Markus Schoder

Peter said:
What is the best way to handle this?

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

Peter Olcott

Markus Schoder said:
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);
}
 
M

Markus Schoder

Peter said:
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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top