什么是远期申报?
What is a forward declaration
本文关键字:什么 更新时间:2023-10-16
我不明白带箭头的那一行到底在做什么。这门课包括那门课吗?
#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
这是QToolButton
类的前向声明(如果你搜索前向声明c++你会发现很多关于这个主题的好搜索结果)
允许使用类QToolButton
作为指针和引用,只要QToolButton
的成员不在特定的头文件中被访问。
前向声明的目的是加快编译速度。如果不使用前向声明,则需要包含QToolButton
的头文件。这意味着,每个包含EnhancedLineEdit
头文件的源文件也间接包含QToolButton
的头文件,即使源文件本身不使用该类,这将减慢编译过程。
所以只有EnhancedLineEdit
的源文件需要包含<QToolButton>
。包含EnhancedLineEdit
头文件的源文件不需要这样做,除非它们想直接使用QToolButton
类。否则,前向声明足以允许在EnhancedLineEdit
的头文件中使用指向QToolButton
的指针。
这一行
class QToolButton;
在当前(似乎是全局的)命名空间中声明类QToolButton
,而不定义类本身。也就是说,这个声明在作用域中引入了类型class QToolButton
。这个类用于类EnchancedLineEdit
private:
QToolButton *clearBtn;
事实上,有一行
就足够了private:
class QToolButton *clearBtn;
^^^^^
,因为它还在当前命名空间中声明了QToolButton
类。
class QToolButton;
和
class QToolButton
在此声明class QToolButton *clearBtn;
中可能使用的称为详细类型名。
在代码片段中,您展示了数据成员声明QToolButton *clearBtn;
在访问class QToolButton
的成员之前,不要求类型QToolButton
是完整的。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- ifstream什么都没读
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 我应该使用什么来代替void作为变体中的替代类型之一
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用