C++12.1.4 类的前向声明、不完全类型类

本文探讨了C++中不完全类型的概念及其应用,包括如何进行类的前向声明,以及这种声明对于定义指向该类型的指针和引用的影响。此外,还讨论了在创建类的对象之前必须完整定义类的原因。
  • 只声明却没有定义的类称为—————–不完全类型,不完全类型不能定义该类型的对象,只能用于定义指向该类型的指针及引用,或者用于声明(不是定义)使用该类型作为形参类型或返回类型的函数。

  • 在创建类的对象之前,必须完整地定义该类。必须定义,而不是声明类,这样,编译器就会给类的对象预定相应的存储空间。同样的,在使用引用或指针访问类的成员之前,必须已经定义类。

 

-可以声明一个类而不定义它:这个声明称为——前向声明,在声明之后,定义之前,类称为——不完全类型,即已知Screen类是一个类型,但不知道包含哪些成员。

class Screen;
  • 只声明却没有定义的类称为不完全类型,不完全类型不能定义该类型的对象,只能用于定义指向该类型的指针及引用,或者用于声明(不是定义)使用该类型作为形参类型或返回类型的函数。

  • 在创建类的对象之前,必须完整地定义该类。必须定义,而不是声明类,这样,编译器就会给类的对象预定相应的存储空间。同样的,在使用引用或指针访问类的成员之前,必须已经定义类。

  • 只有当类定义已经在前面出现过,数据成员才能被制定为该类类型。如果该类型是不完全类型,那么数据成员只能是指向该类类型的指针或引用。

  • 因为只有当类定义体完成后才能定义类,因此类不能具有自身类型的数据成员,然而,只要类名一出现就可以认为该类已经声明,因此,类的数据成员可以是指向自身类型的指针或引用:

class LinkScreen{
    Screen window;
    LinkScreen *next;
    LinkScreen *prev;
};

 

  • static数据成员也可以是该成员所属的类类型。非static成员被限定声明为其自身类对象的指针或引用: 
    《12.6 static类成员》
class Bar{
public:
private:
   static Bar mem1;//ok
   Bar *mem2;//ok
   Bar mem3;//no!!!!因为Bar是一个不完全类型,所以只能定义它的非static的指针或引用。
};

类的前向声明一般用来编写相互依赖的类。13.4

The Best-Selling Programmer Resource–Now Updated for C++11 The C++ standard library provides a set of common classes and interfaces that greatly extend the core C++ language. The library, however, is not self-explanatory. To make full use of its components - and to benefit from their power - you need a resource that does far more than list the classes and their functions. The C++ Standard Library - A Tutorial and Reference, 2nd Edition describes this library as now incorporated into the new ANSI/ISO C++ language standard (C++11). The book provides comprehensive documentation of each library component, including an introduction to its purpose and design; clearly written explanations of complex concepts; the practical programming details needed for effective use; traps and pitfalls; the exact signature and definition of the most important classes and functions; and numerous examples of working code. The book focuses on the Standard Template Library (STL), examining containers, iterators, function objects, and STL algorithms. You will also find detailed coverage of strings, concurrency, random numbers and distributions, special containers, numerical classes, internationalization, and the IOStreams library. An insightful introduction to fundamental concepts and an overview of the library will help bring newcomers quickly up to speed. A comprehensive index will support the C++ programmer in his/her day-to-day life. The book covers all the new C++11 library components, including Concurrency Fractional arithmetic Clocks and Timers Random numbers and distributions New smart pointers Regular expressions New STL containers, such as arrays, forward lists, and unordered containers New STL algorithms Tuples Type traits and type utilities The book also examines the new C++ programming style and its effect on the standard library, including lambdas, range-based for loops, and variadic templates. An accompanying Web site, including source code, can be found at http://www.josuttis.com/.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值