Author Topic: Forward declarations  (Read 4818 times)


  • Guest
Forward declarations
« on: January 22, 2006, 12:54:31 am »
Hi I have a question. What in the world are forward declarations, and why are they needed? Sorry that this is such a dumb question.....

Code: C++
  1. #ifndef EDITOR_H
  2. #define EDITOR_H
  4. #include <wx/wxscintilla.h>
  5. #include <wx/hashmap.h>
  6. #include <wx/datetime.h>
  8. #include "settings.h"
  9. #include "editorbase.h"
  10. #include "printing_types.h"
  12. extern const wxString EDITOR_MODIFIED;
  14. // forward decls
  15. struct cbEditorInternalData; // this is the private data struct used by the editor.
  16. class cbEditor;
  17. class ProjectFile;
  18. class EditorColorSet;
  19. class wxNotebook;
  21. class cbStyledTextCtrl : public wxScintilla
  22. {
  23.     ...
  24. };
  26. #endif // EDITOR_H

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1441
    • CenizaSOFT
Re: Forward declarations
« Reply #1 on: January 22, 2006, 01:23:01 am »
Forward declarations are used to tell the compiler something exists when that's all it needs to know, or that something is somewhere else. In other words, it just gives the compiler enough information to do its job.

Sometimes they're used to don't bother the compiler with all the "internals" of a specific type or function. When it's used to tell the compiler it's somewhere else, it just lets the linker do the dirty job of finding it and putting in the final object/shared/executable file.

In your code snippet there's a forward declaration for wxNotebook. If everything in that header file just needs to know it exists, it's a type, it's just a class, nothing else like it occupies x bytes, it has these methods, inherits from this and that other class, then that fordward declaration reduces parsing times.

There's something important there: "... it occupies x bytes...". If you're going to use that type inside another class/struct, then you need to provide more information, if needed. The exception is when you have references and pointers (doesn't matter the type, the compiler knows their size). Of course you cannot call member functions or use member variables if all you have is the forward declaration (the compiler wouldn't have enough information to know if such a function or variable exists there).

But it'sn't just for fun or reduce parsing times, it's sometimes used because it's needed. Try this:

Code: C++
  1. // header.h
  2. int x;

Code: C++
  1. // source1.cpp
  2. #include "header.h"

Code: C++
  1. // source2.cpp
  2. #include "header.h"

Code: C++
  1. // main.cpp
  2. #include "header.h"
  4. int main()
  5. {
  6.   // does nothing
  7. }

Compile source1.cpp to an object file. Compile source2.cpp to an object file. Compile main.cpp to an object file. Now, try to link those 3 object files and make an executable.

For every object file x exists (it has external linkage). Now, when the linker tries to link those three object files it'll find x three times. What's it supposed to do? Which x should be put in the final file?

Now, change header.h to this:
Code: C++
  1. // header.h
  2. extern int x;

And add this file:
Code: C++
  1. // header.cpp <-- nevermind the name
  2. // include header.h if you want or need to
  3. int x;

Try to create the object files again, plus header.cpp, and link them.

Now, header.h says: there's a variable named x, type int, but isn't here. header.cpp says: I have it.

For more information search about linkage.