I do not have much experience with C++ and I have a question regarding the following lines from Qt documentation here : http://qt-project.org/doc/qt-4.8/mainwindows-application-mainwindow-h.html ( Lines 4-6 after the comment at the top )
class QAction;
class QMenu;
class QPlainTextEdit;
Since QAction, QMenu and QPlainTextEdit are library classes of Qt ( clicking on them leads to their documentation pages), shouldn’t they be included using ‘#include ‘ ? What purpose does just declaring them with the keyword ‘class’ serve ? As far as I think , the compiler will think of it as a completely new class, having nothing to do with the library class QAction.
However, the ‘mainwindow.cpp’ file ( http://qt-project.org/doc/qt-4.8/mainwindows-application-mainwindow-cpp.html ) does not contain any definition of class QAction, though its objects are being used in the code.
What is going on here ?
Edit : Further explanation of the problem
Look into the createActions method of class MainWindow ( http://qt-project.org/doc/qt-4.8/mainwindows-application-mainwindow-cpp.html ). Here objects of class QAction are being created but nowhere can I find a definition of QAction class.
Those are known as “forward declarations” of the classes. It is used to make the compiler know that those symbols refer to classes, which are going to be defined afterwards. Those can be used in a header file to declare a symbol to be a class avoiding an inclusion of its header, which might be needed in some cases (circular inclusions for instance).
You can’t always use forward declarations anyway, because the compiler might need to know how a class is defined. In other cases instead, the compiler needs only to know the fact that it actually is a class.
EDIT: To answer the following question you added, have a look at the QtGui file you can find in *qt_install_dir*/include/QtGui. In my Qt 4.8 installation I see inside:
Then look inside qaction.h: you’ll see the complete definition of the QAction class. The inclusion of the header file QtGui is right at the beginning of your mainwindow.cpp, which makes QAction a complete type.