1

I don't understand what the line with the arrow is doing exactly. Is it including that class in this class?

#ifndef LINEEDIT_H
#define LINEEDIT_H

#include <QLineEdit>

class QToolButton; <--------------------------------------

class EnchancedLineEdit : public QLineEdit
{
    Q_OBJECT

public:
    EnchancedLineEdit(QWidget *parent = 0);
    ~EnchancedLineEdit();

protected:
  void resizeEvent(QResizeEvent *);

private:
    QToolButton *clearBtn;

private slots:
    void updateCloseButton(const QString &text);
};

#endif // LINEEDIT_H
smerlin
  • 6,446
  • 3
  • 35
  • 58
Evan B
  • 73
  • 7

2 Answers2

2

That is a forward declaration of the class QToolButton (if you google forward declaration c++ you will find plenty of good search results regarding this topic)

This allows the usage of the class QToolButton for pointers and references, as long the members of QToolButton are not accessed in that particular Header file.

The purpose of forward declarations is to speed up compilation. If you dont use forward declarations, you would need to include the Header file of QToolButton. This would mean, that every Source file which inclues the Header file of EnhancedLineEdit also indirectly includes the Header file of QToolButton, even if that class is not used in the Source file itself, which will slow down the compilation process.

So only the source file of EnhancedLineEdit will need to include <QToolButton>. Source files which include the header file of EnhancedLineEdit wont need to do so, unless they want to make direct use of the class QToolButton themselves. Otherwise the forward declaration is sufficient to allow the usage of pointers to QToolButton in the header file of EnhancedLineEdit.

smerlin
  • 6,446
  • 3
  • 35
  • 58
1

This line

class QToolButton;

declares class QToolButton in the current (it seems global) namespace without definition of the class itself. That is this declaration introduces type class QToolButtonin the scope. This class is used in the definition of class EnchancedLineEdit

private:
    QToolButton *clearBtn;

In fact it is enough to have line

private:
    class QToolButton *clearBtn;
    ^^^^^

because it also declares class QToolButton in the current namespace.

These specifications

class QToolButton;

and

class QToolButton

that might be used in this declaration class QToolButton *clearBtn; are called elaborated type names.

In the code snippet you showed the data member declaration

QToolButton *clearBtn;

does not require that type QToolButton would be complete until you will access members of class QToolButton.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335