关于不完整类型:

关于不完整类型:

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

 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;   //同上
     };                                    //到这里类的定义才算完整



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值