Vishesh Handa's personal website

Forward Declaration Woes

I’m ashamed to admit it, but I’ve never really understood forward declaration in C++. I know it’s used to speed up compile times, and it helps me fix cyclic dependency issues, but I’ve never really understood it. To this date, my approach of forward declaration has been - “Forward declare most of the things, and then include header files to fix compiler warnings.” I know that’s not the best approach, and I should have read about it earlier, but …

Here are the cases where you can’t use forward declaration -

I knew about the Base Class condition, but not about the other one. These conditions exist because the compiler must be able to find the total size of the class from the its definition. The compiler knows the size of pointers (and references.)

Another issue that had been bugging me was the use of forward declaration with templates. After reading and experimenting this is what I figured out.

A couple of other minor details - Maybe this blog post will help some poor bloke like me, who never bothered to fully understand forward declaration. :-/