不完全类声明(仅限 C++)
不完整的类声明 是不定义任何类成员的类声明。 在声明完成之前,不能声明类类型的任何对象或引用类的成员。 但是,不完整的声明允许您在其定义之前对类进行特定引用,只要不需要该类的大小。
例如,您可以在结构
second的定义中定义指向结构 first 的指针。 结构 first 在 second的定义之前声明为不完整的类声明,并且结构 second 中 oneptr 的定义不需要 first的大小:struct first; // incomplete declaration of struct first
struct second // complete declaration of struct second
{
first* oneptr; // pointer to struct first refers to
// struct first prior to its complete
// declaration
first one; // error, you cannot declare an object of
// an incompletely declared class type
int x, y;
};
struct first // complete declaration of struct first
{
second two; // define an object of class type second
int z;
};但是,如果您使用空成员列表来声明类,那么它是完整的类声明。 例如:
class X; // incomplete class declaration
class Z {}; // empty member list
class Y
{
public:
X yobj; // error, cannot create an object of an
// incomplete class type
Z zobj; // valid
};