Incomplete class declarations.
Problem: Two classes reference each other.
Figure 11.7. Example of two classes referencing each other
Here's a fist attempt at an implementation in C++
// this is Bomb.h #include "Timer.h" class Bomb { private: Timer *timer; }
// this is Timer.h #include "Bomb.h" class Timer { private: Bomb *bomb[8]; // This works due to what is // stated about the * later }
Alternate notion:
// This is an alternate Timer.h #include "Bomb.h" #include <vector> class Timer { private: vector<Bomb*> bomb; // Much safer :) }
Discussion: What seems to be the problem? How could you solve this?
Answer in C++: Incomplete class definitions.
Works when we only need to know that there is a class with a certain name, but do not need to know any details (like in the class definition / header file).
In this case we can tell C++ that there is a class with that name, and that it will be defined elsewhere.
Notion: class classname semicolon. Example: class Timer;
Example:
// this is Bomb.h class Timer; class Bomb { private: Timer *timer; }
// this is Timer.h class Bomb; class Timer { private: Bomb *bomb[8]; }