Doubt regarding 'Find Files' example - No forward declaration of class?
Hi people,
I'm a beginner in C++ and recently started learning Qt. I was just going through the QT 'Find Files' example when I got this doubt. It is regarding some elements of the 'window.h' header file which is part of the 'Find Files' example.
First let me post the source of the window.h file. Then I'll ask my doubt.
window.h
Code:
#ifndef WINDOW_H
#define WINDOW_H
#include <QDialog>
{
Q_OBJECT
public:
private slots:
void browse();
void find();
private:
void createFilesTable();
};
#endif
Doubt Questions:
As you can see above, all the classes of objects that have been used in the header file have been forward declared (like QLabel, QPushbutton, QDir etc. etc.). But in line no. 24, an object of class QStringlist has been declared without any forward declaration of the class QStringlist. A similar case can be found in line no. 17 where the function takes a argument of the type QWidget.
Can anyone explain why these two classes were not forward declared?
Regards,
Re: Doubt regarding 'Find Files' example - No forward declaration of class?
Ok, the widget thing is easy. QDialog depends on QWidget, so inside qdialog.h qwidget.h is included, thus you can use QWidget. QStringList puzzles me too.
Re: Doubt regarding 'Find Files' example - No forward declaration of class?
ok, in qwidget.h they include qfont.h. And in qfont.h they have included qstring.h. And somewhere in there they surely have included qlist.h
Re: Doubt regarding 'Find Files' example - No forward declaration of class?
Ok, thanks for the reply. I got the QWidget part ( According to Qt Assistant, QDialog inherits QWidget, so the latter is automatically available when QDialog is included)
**Damn! Why didn't I check Qt Assistant before asking?? Sorry about that.**
But I'm still confused about the QStringlist part. You said that QWidget inherits QFont( which further inherits QString and QStringlist), but nothing such is mentioned in the reference document of QWidget. In fact, I checked the reference document of QStringlist and to my surprise, this class is not inherited by any other class.
So what now mate?
Re: Doubt regarding 'Find Files' example - No forward declaration of class?
The version of window.h in the 4.6 docs (here) looks like this:
Code:
#ifndef WINDOW_H
#define WINDOW_H
#include <QDialog>
#include <QDir>
. . .
And in qdir.h you have:
Code:
#ifndef QDIR_H
#define QDIR_H
#include <QtCore/qstring.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qstringlist.h>
That example won't compile on my machine with QDir forward declared.
Re: Doubt regarding 'Find Files' example - No forward declaration of class?
@norobro : Thanks for the reply. Problem solved. So, it seems the class QDir has QStringlist 'included' in it rather than inheriting it.
Also, the window.h in your post belongs to version 4.6. Mine is of version 4.2. You can find it here. But I guess the 4.2 version will compile fine because if you notice, no new object of class QDir is being declared in the version I quoted. It will give error only if any non-pointer declaration takes place in the code(as in the version you quoted). But I'm not sure about this.
Re: Doubt regarding 'Find Files' example - No forward declaration of class?
The Google C++ Style Guide (link) has some good information.
Here's what it says about forward declarations:
Quote:
How can we use a class Foo in a header file without access to its definition?
We can declare data members of type Foo* or Foo&.
We can declare (but not define) functions with arguments, and/or return values, of type Foo. (One exception is if an argument Foo or const Foo& has a non-explicit, one-argument constructor, in which case we need the full definition to support automatic type conversion.)
We can declare static data members of type Foo. This is because static data members are defined outside the class definition.
On the other hand, you must include the header file for Foo if your class subclasses Foo or has a data member of type Foo.
Re: Doubt regarding 'Find Files' example - No forward declaration of class?
One golden tip I can give is:
Don't rely on include files including other files.
Always try to explicitly include the header or forward declaration of any class you use.
In that respect, and my personal opinion, the example isn't 100% correct.
Re: Doubt regarding 'Find Files' example - No forward declaration of class?
You surely find all header files on your disk, where you have installed Qt. That should be the fastest and easiest why to find the files.