QT中类的前向声明

    技术2024-12-30  56

    前向声明(forward declaration):刚开始接触QT编程就会看到如下的代码:头文件中:1 #ifndef FINDDIALOG_H2 #define FINDDIALOG_H3 #include <QDialog>4 class QCheckBox;5 class QLabel;6 class QLineEdit;7 class QPushButton;8 class FindDialog : public QDialog9 {10     Q_OBJECT11 public:12     FindDialog(QWidget *parent = 0);13 signals:14     void findNext(const QString &str, Qt::CaseSensitivity cs);15     void findPrevious(const QString &str, Qt::CaseSensitivity cs);16 private slots:17     void findClicked();18     void enableFindButton(const QString &text);19 private:20     QLabel *label;21     QLineEdit *lineEdit;22     QCheckBox *caseCheckBox;23     QCheckBox *backwardCheckBox;24     QPushButton *findButton;25     QPushButton *closeButton;26 };27 #endif这里开头的类声明就叫作类的前向声明。前向声明的作用是告诉编译器这个类的存在,由于这里只是使用了这些类的指针,编译器不用关心这些类的具体定义。使用前向声明后在这里就不用包含相应的头文件。但是在CPP文件中,如果要对这些前向声明类的指针进行实例化就必须包含相关的头文件,一般在CPP文件中会包含<QtGui>,这个库就包含了这些类的声明。据说这样做的好处是加快编译速度。

    这里还有一篇关于类的前向声明的文章http://blog.csdn.net/todototry/archive/2007/04/12/1562394.aspx

    最新回复(0)