关于不完整类型:

来源:互联网 发布:paparecipeq正品软件 编辑:程序博客网 时间:2024/04/29 22:32

关于不完整类型:

我们可以只是声明而不真正地定义一个类,比如

 class Screen;

这样做的含义:

“This declaration, sometimes referred to as a forward declaration , introduces the name Screen into the program and indicates that Screen refers to a class type. After a declaration and before a definition is seen, the type Screen is an incompete type it's known that Screen is a type but not known what members that type contains.”

 这句很重要:

“An incomplete type may be used to define only pointers or references to the type or to declare (but not define) functions that use the type as a paremeter or return type.”

这是很容易犯的错误,而且如果不知道这个知识,当面临error的时候也不知所措。一个不完整类型不可以用来直接定义对象,但可以用来定义一个指向该不完整类型的指针或者一个与该类型绑定的引用,而且还可以用来声明一个函数,这个函数使用了这个不完整类型作为形参或者返回值。记住,这里指的是声明,也就直接加分号,不可以有定义。

“Because a class is not defined until its class body is complete, a class cannot have data members of its own type. However, a class is considered declared as soon as its class name has been seen. Therefore, a class can have data members that are pointers or references to its own type:”

class LinkScreen {
         Screen window;
         LinkScreen *next;    //到这里的时候,该类并没完整,但这个时候可以定义一个指向该类的指针,与上面所述一致
         LinkScreen *prev;   //同上
     };                                    //到这里类的定义才算完整



原创粉丝点击