class className in another class header file

I have seen in many code samples this specific code declaration in a header file that defines a class and its properties and methods

1
2
3
4
5
6
7
8
9
10
11
class Game;
class Window;

class Application
{
public:
 properties...
 methods.......
}



The classes Game and Window are defined in a header and cpp file of their own. Apparently the Application.h uses those 2 classes. My question is what it different from just using #include "Window.h" and "Game.h"?
class Window; means "I promise to the compiler that there is a class named 'Window', but I'm not going to tell you anything about it at this point"

#include "Window.h" means "please open the text file named 'Window.h' and copy and paste everything inside it to right here where I wrote the #include." There could be anything at all in that text file. It could be just the word "bananas" written out a thousand times. It could be anything.

So very, very different indeed.
Last edited on
Yeah i kinda guessed that is the distinction that in the latter case, you copy the whole code in the file, but does their purpose is kinda the same? So the first case is less reliable and you leave up to the linker do the job. But in the end, you need somehow to link those files and classes. Another question is, what are the advantages and disadvantages on each case and when should they be used. I could guess you might want the first option to reduce the code size, but that is just a simple guess, it might be more than that. However they do get included in the cpp file
Last edited on
If the compiler needs to know more than just the fact that the class eixsts, you need to tell it more. This is comonly done by providing the whole declaration of the class. This is done by typing it in.

If the compiler, in order to compile the current source file, doesn't need to know any more than the simple fact that the class exists, you can save yourself typing and save the compiler work by just telling the compiler that the class exists without providing the whole declaration.

#include is a shortcut to save you typing and to help you organise your code into neat little files. This is handled before the compiler actually gets to see the code.

As far as the compiler and linker are concerned, "Windows.h" does not exist.

You should read this: https://www.daniweb.com/programming/software-development/tutorials/466177/understanding-c-from-source-to-binaries
Last edited on
Suppose an application refers to a window:
1
2
3
4
class Application:
   Window *mainWindow;
    ...
};

and a window refers to the application that it belongs to :
1
2
3
4
class Window {
    Application *app;
    ...
};

Which class do you declare first? Class Application needs class Window, so you declare Window first, but Window needs Application so you declare Application first. Ack!!!

The solution is a forward declaration.

1
2
3
4
5
6
7
8
9
10
11
class Window;   // forward declaration. "I promise that class Window will be declared"

class Application:
   Window *mainWindow;
    ...
};

class Window {
    Application *app;
    ...
};


You can only create a pointer or a reference to a class with a forward declaration. The key is that the compiler needs to know how big the class is otherwise. It knows the size of pointers because they're always the same, regardless of what they point to.
Topic archived. No new replies allowed.