I am looking for the definition of when I am allowed to do forward declaration of a class in another class's header file:
Am I allowed to do it for a base class, for a class held as a member, for a class passed to member function by reference, etc. ?
Take it that forward declaration will get your code to compile (obj is created). Linking however (exe creation) will not be successfull unless the definitions are found.
The main rule is that you can only forward-declare classes whose memory layout (and thus member functions and data members) do not need to be known in the file you forward-declare it.
This would rule out base classes and anything but classes used via references and pointers.
I just want to add one important thing you can do with a forwarded class not mentioned in the answer of Luc Touraille.
What you can do with an incomplete type:
Define functions or methods which accept/return pointers/references to the incomplete type and forward that pointers/references to another function.
A module can pass through an object of a forward declared class to another module.
In file in which you use only Pointer or Reference to a class.And no member/member function should be invoked thought those Pointer/ reference.
with
class Foo;
//forward declarationWe can declare data members of type Foo* or Foo&.
We can declare (but not define) functions with arguments, and/or return values, of type Foo.
We can declare static data members of type Foo. This is because static data members are defined outside the class definition.
The general rule I follow is not to include any header file unless I have to. So unless I am storing the object of a class as a member variable of my class I won't include it, I'll just use the forward declaration.
As long as you don't need the definition (think pointers and references) you can get away with forward declarations. This is why mostly you'd see them in headers while implementation files typically will pull the header for the appropriate definition(s).