《C++标准程序库》第三章摘录与笔记

《C++标准程序库》第三章摘录与笔记

3.2 头文件

C++标准库在定义标准头文件时,对头文件扩展名进行了规范:根本没有扩展名。这种写法也适用于C标准头文件。但原来在C标准库中的头文件,必须采用前缀字符C,而不再是扩展名.h,在这些头文件中,每一个标识符都被声明在std命名空间中。这种命名方式的优点之一是可以区分旧头文件中的char* C函数,和新头文件中的标准C++ string类:
#include <string> // C++ class string
#include <cstring> // char* functions from C
为了向下兼容C,旧式的C标准头文件仍然有效。如使用#include <stdio.h>。不过此时,包含头文件中的标识符是同时声明于全局范围和namespace std中。
至于<iostream.h>这一类C++旧式头文件,C++标准并未加以规范,意味着不在支持这些头文件。只是大多数C++标准的实现版本中还对其进行支持。但为了可移植性以及统一性,最好还是不用了吧。
《MSDN》摘录:
The C++ language and the Standard C++ Library support two types of strings:
Null-terminated character arrays often referred to as C strings.
Template class objects, of type basic_string, that handle all char-like template arguments.

3.3 错误处理和异常处理

C++标准程序库中各组件对于错误和异常处理的设计与实现有一定的差异,主要是考虑效率与安全的侧重点不同吧。

3.3.1标准异常类型

语言本身或标准程序库所抛出的所有异常,都派生自基类exception。这是其他数个标准异常类型的基类,他们共同构成一个类体系。如图:


这些标准异常类别可分为三组:
1.语言本身支持的异常:bad_alloc、bad_cast、bad_typeid、bad_exception。
2.C++标准程序库发出的异常(总是派生自logic_error): invalid_argument、length_error、out_of_range、domain_error,标准程序库的I/O部分提供了一个ios_base::failure的特殊异常。
3.程序作用域之外发出的异常(总是派生自runtime_error):range_error、overflow_error、underflow_error。
标准程序库的实现产品也可能提供其他的异常类型,但为了移植性,还是最好只是用标准异常。
异常类别的头文件
基础类别exception和bad_exception定义于<exception>。bad_alloc定义于<new>。bad_cast和bad_typeid定义于<typeinfo>。ios_base::failure定义于<ios>。其他异常类型都定义于<stdexcept>。
《MSDN》摘录:
The logical errors are caused programmer mistakes. 
The run-time errors occur because of mistakes in either the library functions or in the run-time system.

3.3.2 异常类型的成员

所有标准异常的接口只含一个成员函数:what(), 用以获取“类型本身以外的附加信息”。

3.3.3 抛出标准异常

你可以在自己的程序库或程序内部抛出某些标准异常(符合此类的标准异常,生成时都只需要一个string参数,它将成为被what()返回的描述字符串。)。提供这种功能的标准异常有:logic_error以其派生类型、runtime_error及其派生类型、ios_base::failure。你不能抛出exception,也不能抛出 任何用以支持语言核心性质的异常(前面的第一类)。
想要抛出一个标准异常,只需生成一个描述该异常的字符串,并将它初始化,交给异常对象:
std::string s;
...
throw std::out_of_range(s);
由于char* 可被隐式转换为string,所以你可以直接使用该字符串字面量:

throw std::out_of_range("out_of_range(somewhere, somehow)");

#include <iostream>
using namespace std;

int main( )
{
   try 
   {
      throw logic_error( "logic error" );
   }
   catch ( exception &e ) 
   {
      cerr << "Caught: " << e.what( ) << endl;
      cerr << "Type: " << typeid( e ).name( ) << endl;
   };
}
输出:

Caught: logic error
Type: class std::logic_error

3.3.4 从标准异常类型中派生新类型

另一个在程序中采用标准异常类型的可能情况是,定义一个直接或间接派生自exception的特定异常类型。要这么做,必须确保what()机制正常运作。
namespace MyLib
{
	// user-defined exception class derived from a standard class for exceptions
	class MyProblem : public std::exception
	{
	public:
		...
		MyProblem(...)	// special constructor
		{
		}
		virtual const char* what() const throw()	// what() function
		{
			...
		}
	};
	...
	void f()
	{
		...
		// create an exception object and throw it
		throw MyProblem(...);
	}
}
提供what()函数的另一种方法是,令你的异常类型派生自3.3.3节中所描述的标准异常:
namespace MyLib
{
	// user-defined exception class derived from a standard class for exceptions
	// that has a constructor for the what() argument
	class MyRangeProblem : public std::out_of_range
	{
	public:
		MyRangeProblem(const string& whatString)
		: out_of_range(whatString)
		{
		}
	};
	...
	void f()
	{
		...
		// create an exception object by using a string constructor and throw it
		throw MyRangeProblem("here is my special range problem");
		...
	}
}

3.4 配置器(Allocators)

C++标准程序库中处理内存配置和寻址的对象。
C++标准程序库定义了一个缺省配置器:
namespace std
{
	template <class T>
	class allocator;
}
缺省配置器可以再任何“配置器得以被当做参数使用”的地方担任默认值。缺省配置器会执行内存分配和回收的一般性方法,也就是呼叫new和delete操作符。
实际生活中最典型的方式是直接采用缺省配置器。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值