2.10.  Class Declarations and Definitions

[ fromfile: classdeclarations.xml id: classdeclarations ]

Figure 2.4.  Bidirectional Relationship

Bidirectional Relationship

Example 2.12. src/circular/badegg/egg.h

[ . . . . ]
#include "chicken.h"
class Egg {
 public:
    Chicken* getParent(); 
};
[ . . . . ]

Example 2.13. src/circular/badegg/chicken.h

[ . . . . ]
#include "egg.h"

class Chicken {
 public:
    Egg* layEgg();
};
[ . . . . ]

Example 2.14. src/circular/goodegg/egg.h

[ . . . . ]
class Chicken;              1
class Egg {
 public:
    Chicken* getParent();   2 
};
[ . . . . ]

1

Forward class declaration.

2

Okay in declarations if they are pointers.


Example 2.15. src/circular/goodegg/egg.cpp

#include "chicken.h"
#include "egg.h"

Chicken* Egg::getParent() {
    return new Chicken(); 1   
}

1

Requires definition of Chicken.


For further details, see Section C.2