Forward declarations for objects that are not widgets
isaacEnrique last edited by
I'm developing a GUI tool and I am using forward declarations for widgets in my class (QPushButton, QToolButtons, QLabel, ect). I understand that use forward declarations reduces compile time, so I use them whenever I can, is this an error?.
Based on the above, my question is whether it is desirable to use forward declarations to data members that are not widgtes (QVector, QList, QRect, QImage, ect)?. Particularly, I need you in my class one QVector<QRectF> ... What is better, declare a member variable of that type directly or use forward declarations and pointers?
Thanks in advance for any help and / or suggestions.
Any class which you would like to use should be included in with #include <QPushButton>. It is about resolving usage of particular class. If you are using the QVector, QImage etc, you should include them in header file.
In general if you have class and using it, defined else where you should include. Forward declaration is generally used when you are writing your own class in and whose definition is somewhere else.
isaacEnrique last edited by
I have seen in several places (including the Qt examples that come with QtCreator) using forward declarations for Qt classes themselves. Sometimes directly:
and sometimes using the macro QT_FORWARD_DECLARE_CLASS(). Member variables using pointers is then declared.
According to what I've read, the reason is that (among other things) reduce the time of compilation of programs.
However, I've only seen it used for widgets, and so I have doubts about whether it is advisable to use forward declarations for other classes (no only widgets classes). Common sense makes me think that for those other classes also results in a reduction in compile time, however, I don't know consider whether that reduction in the time of compilation worthwhile for all cases, for example, using forward declarations, I will have QVector* instead of QVector.
If I properly understood what you told me, you suggest me to forget completely forward declarations and include in my header files all I need (<QLabel>, <QPushButton>, ect)... Or am I wrong?
Forward declarations become useful when you are developing a very large project where compilation can take many minutes (or even hours). However, you can't really feel any benefits when your project is small.
Remember also: Forward declarations save you compilation time, but costs you editing time. You need to write
_class MyClass;_in the .h file and
_#include "myclass.h"_in the .cpp file. If you later decide that you don't want to use MyClass anymore, you'll need to remove 2 lines from 2 files.
[quote]for example, using forward declarations, I will have QVector* instead of QVector.[/quote]Do NOT use pointers just so that you can use forward declarations. You should design your classes first, and THEN see which components can be forward-declared. Not everything should be forward-declared.
In particular, Qt's container classes (QVector, QString, QByteArray, etc.) are value-based classes; you rarely need to use pointers with them.
cincirin last edited by
You can use forward declaration for variables only for pointers and references. You must include the header if variable is not declared as pointer or reference. Besides compilation speed forward declaration is used for mutual recursion.