P
Peter Olcott
What is the best way to handle this?
What is the best way to handle this?
Peter said:What is the best way to handle this?
Markus Schoder said:Forward declaration and implementing the inline functions after both
class declarations with the inline specifier.
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);
};
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);
}
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.