Chapter 37. Forward Declarations

Difficulty: 3

Forward declarations are a great way to eliminate needless compile-time dependencies. But here's an example of a forward-declaration snare. How would you avoid it?

  1. Forward declarations are very useful tools. In this case, they don't work as the programmer expected. Why are the marked lines errors?

    // file f.h
    //
    class ostream;  // error
    class string;   // error
    string f( const ostream& );
    
  2. Without including any other files, write the correct forward declarations for ostream and string above.

Solution

Solution
  • 1. Forward declarations are very useful tools. In this case, they don't work as the programmer expected. Why ...

Get More Exceptional C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.