Part V: Advanced Topics
file:///C|/...+%20Primer%B5%DA%CB%C4%B0%E6%D6%D0%D3%A2%CE%C4%B6%D4%D5%D5/0201721481/part05.html[2008-12-4 22:47:55]
Part V: Advanced Topics
第五部分 高级主题
Part V covers additional features that, although useful in the right context, are not needed by every C++ programmer. These
features divide into two clusters: those that are useful for large-scale problems and those that are applicable to specialized
problems rather than general ones.
第五部分涵盖了一些高级特征,虽然它们在适当情况下是有用的,但不是每个 C++ 程序员都需要。这些特征分为两类:用于规模问题,以及应用于特殊问题
的。
Chapter 17 covers exception handling, namespaces, and multiple inheritance. These features tend to be most useful in the context
of large-scale problems.
第十七章涵盖了异常处理、命名空间和多重继承。这些特征在大规模问题的环境中最有用。
Even programs simple enough to be written by a single author can benefit from exception handling, which is why we introduced the
basics of exception handling in Chapter 6. However, the need to deal with unexpected run-time errors tends to be more important
and harder to manage in problems that require large programming teams. In Chapter 17 we review some additional useful
exception-handling facilities. We also look in more detail at how exceptions are handled and the implications of exceptions on
resource allocation and destruction. We also show how we can define and use our own exception classes.
即使是可由一个开发人员编写的简单程序也可以从异常处理中获益,这是我们之所以要在第六章介绍异常处理基础知识的原因。但是,在需要大的编程团队的
问题中,处理不可预期的运行时错误变得更加重要,更难管理。第十七章将讨论另外一些有用的异常处理设施,还将更详细地介绍怎样处理异常、资源分配和
回收异常的含义,以及怎样定义和使用自己的异常类。
Large-scale applications often use code from multiple independent vendors. Combining independently developed libraries would be
difficult (if not impossible) if vendors had to put the names they define into a single namespace. Independently developed libraries
would almost inevitably use names in common with one another; a name defined in one library would conflict with the use of that
name in another library. To avoid name collisions, we can define names inside a namespace.
大规模应用程序经常使用来自多个独立供应商的代码,如果将供应商定义的名字都放在一个命名空间,组合独立开发的库即使不是完全不可能,将是很困难
的。独立开发的库几乎不可避免地会使用彼此相同的名字,一个库中定义的名字可能会与其他库中的相同名冲突。为了避免名字冲突,可以将名字定义在
namespace 内。
Right from the beginning of this book we have used namespaces. Whenever we use a name from the standard library, we are using
a name defined in the namespace named std. Chapter 17 shows how we can define our own namespaces.
其实从本书一开始我们已经使用了命名空间。只要用到标准库中的名字,其实都是在使用名为 std 的命名空间中的定义的名字。第十七章将介绍怎样自定义
命名空间。
Chapter 17 closes by looking at an important but infrequently used language feature: multiple inheritance. Multiple inheritance is
most useful for fairly complicated inheritance hierarchies.
第十七章的最后介绍了一个重要但不常用的语言特征——多重继承。多重继承对于相当复杂的继承层次最为有用。
Chapter 18 covers several specialized tools and techniques. These tools and techniques are applicable to particular kinds of
problems.
第十八章讨论了几个特殊的工具和技术,这些工具和技术可应用于某些特定类型的问题。
The first part of Chapter 18 shows how classes can define their own optimized memory management. We next look at C++ support
for run-time type identification (RTTI). These facilities let us determine the actual type of an object at run-time.
第十八章的第一节介绍了类怎样自定义优化内存管理,接着介绍 C++ 对运行时类型识别(RTTI)的支持,这种设施使我们能够在运行时确定对象的实际类
型。
Next, we look at how we can define and use pointers to class members. Pointers to class members differ from pointers to ordinary
data or functions. Ordinary pointers only vary based on the type of the object or function. Pointers to members must also reflect
the class to which the member belongs.
接下来,介绍怎样定义和使用类成员的指针。类成员的指针不同于指向普通数据或函数的指针,普通指针只根据对象或函数的类型而变化,而成员的指针还必
须反映成员所属的类。
We then look at three additional aggregate types: unions, nested classes, and local classes.
然后,介绍另外三种聚合类型:联合、嵌套类和局部类。
The chapter closes by looking briefly at a collection of features that are inherently nonportable: the volatile qualifier, bit-fields, and
linkage directives.