为什么使用类的前向声明
1. 什么是前向声明?
前向声明就是: 可以声明一个类而不定义它, 这个声明,有时候被称为前向声明(forward declaration),在程序中引入了类类型的Screen.在声明之后,定义之前,类Screen是一个不完全类型(incompete type),即已知Screen是一个类型,但不知道包含哪些成员.
class Screen;//declaration of the Screen class
不完全类型只能以有限方式使用,不能定义该类型的对象,不完全类型只能用于定义指向该类型的指针及引用,或者用于声明(而不是定义)使用该类型作为形参类型或返回类型的函数.
2. 为什么需要前向声明?
Suppose you want to define a new class B that uses objects of class A. B only uses references or pointers to A. Use forward declaration then : you don't need to include <A.h> . This will in turn speed a little bit the compilation.
class A ;
class B {
private:
A* fPtrA ;
public:
void mymethod(const& A) const ;
} ;