Coaxial
Coaxial

Reputation: 143

What does "class ClassName;" mean when put in header file of other class definition?

I am a bit confused about this line of code class TreeItem;. class TreeItem; is defined in other files not included in this file. I would must look this question up in a book, but I am sure answer for this question is very simple.

#ifndef TREEMODEL_H
#define TREEMODEL_H

#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>

class TreeItem; //here, what does this do??

//! [0]
class TreeModel : public QAbstractItemModel
{
   ...
public:
   ...
private:
   ...
};
//! [2]

#endif // TREEMODEL_H

Upvotes: 7

Views: 4733

Answers (2)

It is a forward declaration, and it is useful to enable to have pointers to TreeItem-s in some internal structures, etc.

Forward declarations are notably needed when you have co-recursive definitions; e.g. a struct foo containing pointers to struct bar which contains pointers to struct foo, etc... You might even declare these struct-s and define the functions operating on them in different translations units.

It is common practice, both in C and in C++, to forward declare some struct (or class) and to have variables which are pointers to it, and declare (and possibly call) functions handing such pointers.

Of course, some other file (or translation unit) would declare such opaque data structures and implement operations on it.

This is a common way to implement abstract data types.

BTW, there generally is a very common example of that in standard C. The FILE type from <stdio.h> is quite often some opaque (or at least, well hidden by many header files) struct (and you obviously don't need to know the internals of it to use <stdio.h> functions like fprintf)

In your (Qt's) case, you don't want to #include <QTreeItem> -or whatever header file is declaring class TreeItem- in this QTreeModel header file. If you did, that would add an additional header dependency (you don't need to recompile TreeModel code when TreeItem implementation has changed!) and would slow down compilation.

In smaller software projects, you might simply have a single header file and define all the struct inside.

Upvotes: 8

Steephen
Steephen

Reputation: 15824

It is called forward declaration. Wikipedia says as follows:

In C++, classes can be forward-declared if you only need to use the pointer-to-that-class type (since all object pointers are the same size, and this is what the compiler cares about). This is especially useful inside class definitions, e.g. if a class contains a member that is a pointer to another class; to avoid circular references (i.e. that class might also contain a member that is a pointer to this class), we simply forward-declare the classes instead.

Forward declaration of a class is not sufficient if you need to use the actual class type, for example, if you have a member whose type is that class directly (not a pointer), or if you need to use it as a base class, or if you need to use the methods of the class in a method.

Upvotes: 7

Related Questions