Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The requirement for declarations of this sort comes from two forces. The first is that it simplifies compiler design. Since types and variables have the same identifier structure, the compiler must know which it is encountering whenever it does parse an identifier. There are two ways to do this. One way would be to require that every identifier be declared before it may be used in other definitions. This means that the code must forward declare any name it intends to use before giving its definition. This is a very easy way to write a compiler with an otherwise ambiguous grammar. </p> <p>The other way to do this is to handle it in multiple passes. Any time an undeclared identifier is encountered, it is skipped, and the compiler tries to resolve it once it's parsed the whole file. It turns out that the grammar of C++ makes this very difficult to do correctly. Compiler writers didn't want to have to go to this trouble, and so we have forward declarations.</p> <p>The other reason is that you may actually want to have forward declarations so that recursive structures are determinite as an intrinsic property of the language. This is a bit more subtle. Suppose you had written a mutually recursive class network:</p> <pre><code>class Bar; // forward declaration class Foo { Bar myBar; }; class Bar { int occupySpace; Foo myFoo; }; </code></pre> <p>This is obviously impossible, because the <code>occupySpace</code> member would appear in an infinitely nested recursion. requiring that a forward declaration of all members in a definition provides a specific amount of information for this. In particular, it allows the compiler enough information to form a reference to a class, but not to instantiate the class (because it's size is not known). The forward declarations make this a feature of the syntax of the language, much like how lvalues are assignable as a feature of the language syntax rather than a more subtle semantic or run-time requirement. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload