3

I've noticed lines of code like this in the book I'm reading:

namespace sf
{
class RenderWindow;
}

class StateStack;
class Player;

class State
{
    // Code for the class
};

What do the lines with just the class, classname, and semicolon mean?

Connor
  • 129
  • 6
  • 3
    Perhaps this will help: http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration?lq=1 – chris Jul 19 '14 at 00:32

2 Answers2

13

These are forward declarations. They let the following code know that there are classes with the names RenderWindow, StateStack, and Player. This satisfies the compiler when it sees these names used. Later the linker will find the definition of the classes.

Just a learner
  • 26,690
  • 50
  • 155
  • 234
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Ok, so what's the advantage of using this? It seems like it would be easier to just include the file with that class? – Connor Jul 19 '14 at 00:36
  • 2
    @Connor The advantage arises in the case where class A uses class B and class B uses class A. This requires a forward declaration because if you `#include "A.h"` in `B.h` and vice versa, you get an infinite loop as well as duplicate definitions for the same class. – Code-Apprentice Jul 19 '14 at 00:38
  • @Connor It just lets the compiler know it exists without the full class actually being there. – BWG Jul 19 '14 at 00:40
  • 1
    "Later the linker" --> "Later the compiler" . – M.M Jul 19 '14 at 00:54
  • 1
    @MattMcNabb I stand by my original answer. The forward declaration tells the **compiler** that the class exists. The **linker** figures out where the class's code is. – Code-Apprentice Jul 19 '14 at 00:55
  • It's the compiler that finds the definition of the classes. Linking doesn't match up class definitions with class forward declarations (but it does match up function definitions with function declarations, and non-member variable definitions with non-member variable declarations). The class definition is required to be visible for the compiler to compile any code that depends on the class definition. – M.M Jul 19 '14 at 01:01
  • @MattMcNabb If the full definition is required at the point of use, the compiler will complain. Casper Von B makes a good point that forward references are only necessary when the full definition *isn't* required, such as for a pointer or reference. – Code-Apprentice Jul 19 '14 at 01:06
4

It is a forward declaration, essentially it signals to the compiler that the full definition will follow elsewhere.

The primary use case for this is cases where you do not need the full definition, for example if you have a pointer of type T you don't need the full definition of T until instantiation and thus its not required to have for a declaration of T*.

Casper Beyer
  • 2,203
  • 2
  • 22
  • 35
  • This is necessary because of the simplistic nature of the compiler, there is no lookup, if it hasn't been declared in the translation unit it does not exist. – Casper Beyer Jul 19 '14 at 08:52