tfiner/cppug — comment on commit d052fa945521c39813716d49e5c36d880e3648e2
File:  line 0
By tfiner:

Yeah, forward decl's are a must for larger projects. http://en.wikipedia.org/wiki/Forward_declaration

If the compiler doesn't need the size of the class, or any implementation details about it (like member names), then you can use a forward declaration of its name.

Pointers and references are all the same size, so pointer and reference members don't need the definition of what they are referring to, you can forward declare these. You should automatically forward declare every member you can (smart pointers, references, etc.).

You also need the definition of a class if you're calling one of it's members in your header:

**c.h**

```cpp
class M; // fwd decl

class C {
inline void herp() { m->derp(); } // Fails to compile, because the compiler needs a
// complete definition of M. Does it really have a
// derp() member function?

M* m; // This pointer to M is OK.
// The compiler knows that all pointers are the same size and
// has enough information to figure out how to create an object
// of type class C.
};
```

The better thing to do is move herp into a corresponding .cpp file and get rid of the inline keyword:
**c.h**

```cpp
class M; // fwd decl

class C {
void herp(); // Compiles, no knowledge of M required.
M* m;
};
```

**c.cpp**

```cpp
#include "c.h" // where class C is defined.
#include "m.h" // where class M is defined.

void C::herp() { m->derp(); } //compiles.
```