Team LiB
Previous Section Next Section

Chapter Summary

小结

C++ provides several specialized facilities that are tailored to particular kinds of problems.

C++ 讲述了几个专门针对一些特定问题的特殊设施。

Customized memory management is used by classes in two ways: A class may need to define its own internal memory allocation that allows it to streamline allocation of its own data members. A class might want to define its own, class-specific operator new and operator delete functions that will be used whenever new objects of the class type are allocated.

类的自定义内存管理有两种方式:定义自己的内部内存分配,以简化自己的数据成员的分配;定义自己的、类特定的 operator newoperator delete 函数,在分配类类型的新对象时使用它们。

Some programs need to directly interrogate the dynamic type of an object at run time. Run-time type identification (RTTI) provides language level support for this kind of programming. RTTI applies only to classes that define virtual functions; type information for types that do not define virtual functions is available but reflects the static type.

一些程序需要在运行时直接询问对象的动态类型。运行时类型识别(RTTI)为这类程序设计提供语言级支持。RTTI 只适用于定义了虚函数的类,没有定义虚函数的类型的类型信息是可用的但反映静态类型。

Pointers to ordinary objects are typed. When we define a pointer to a class member, the pointer type must also encapsulate the type of the class to which the pointer points. A pointer to member may be bound to any member of the class that has the same type. When we dereference a pointer to member, an object from which to fetch the member must be specified.

普通对象的指针是有类型的。定义类成员的指针的时候,指针类型必须也封装指针所指向的类类型。可以将成员指针绑定到具有相同类型的任意类成员,引用成员指针的时候,必须指定从中获取成员的对象。

C++ defines several additional aggregate types:

C++ 还定义了另外几个聚焦类型:

  • Nested classes, which are classes defined in the scope of another class. Such classes are often defined as implementation classes of its enclosing class.

    嵌套类,它是在另一个类的作用域中定义的类,这样的类经常定义为其外围类的具体实现类。

  • Unions are a special kind of class that may contain only simple data members. An object of a union type may define a value for only one of its data members at any one time. Unions are most often nested inside another class type.

    联合,是只能包含简单数据成员的一种特殊类。union 类型的对象在任意时刻只能为它的一个数据成员定义值。联合经常嵌套在其他类类型内部。

  • Local classes, which are very simple classes defined local to a function. All members of a local class must be defined in the class body. There are no static data members of a local class.

    局部类,是局部于函数而定义的非常简单的类。局部类的所有成员必须定义在类定义体中,局部类没有静态数据成员。

C++ also supports several inherently nonportable features including bit-fields and volatile, which make it easier to interface to hardware, and linkage directives, which make it easier to interface to programs written in other langauges.

C++ 还支持几种固有的不可移植的特征,包括位域和 volatile(它们可使与硬件接口更容易)以及链接指示(它使得与用其他语言编写的程序接口更容易)。

Team LiB
Previous Section Next Section