现在的位置: 首页 > 综合 > 正文

C++标准库阅读笔记

2013年02月01日 ⁄ 综合 ⁄ 共 3859字 ⁄ 字号 评论关闭

p17

Thus, the general rule in C++ is that any identifier of a template is considered to be a value, except it is qualified by typename.

 

p18

By providing a different template type for the member function, you relax the rule of exact match.

 

p19

If the type matches exactly, the implicit copy constructor is generated and called.

 

p20

If you use the syntax of an explicit constructor call without arguments, fundamental types are initialized with zero:

 

p21

You can't process
the exception and continue from where you found the exception. In this regard, exception handling is completely different from signal handling.

 

void f() throw(bad_alloc); //f() may only throw bad_alloc exceptions
void f() throw();//f() does not throw

 

p22

You don't have to qualify the namespace for functions if one or more argument types are defined in the namespace of the function. This rule is called Koenig lookup.

 

p25

Note that these operators are provided for only one argument.

static_cast<Fraction>(15,100); // Oops, creates Fraction(l00)

 

Unlike C, C++ defines an implicit return 0;

p33

The base class exception and class bad_exception are defined in <exception>. Class bad_alloc is defined in <new>. Classes bad_cast and bad_typeid are defined in <typeinfo>. Class ios_base::failure is defined in <ios>. All other classes are defined in <stdexcept>.

p34

The set of standard exceptions that provide this ability contains class logic_error and its derived classes, class runtime_error and its derived classes, as well as class ios_base::failure. Thus, you can't throw exceptions of the base class exception and any exception class that is provided for language support.

p38

A template constructor does not hide the implicitly generated default constructor.

p42
smart pointer is a local variable,it gets destroyed automatically when the function is exited regardless of whether the exit is normal or is due to an exception. The class auto_ptr was designed to be such a kind of smart pointer.

you must initialize the auto_ptr directly by using its value
std::auto_ptr<ClassA> ptr1(new ClassA);//OK
std::auto_ptr<ClassA> ptr2 = new ClassA;//ERROR

p69

exit() destroys all static objects, flushes all buffers, closes all I/O channels, and terminates the program (including calling atexit() functions). If functions passed to
atexit() throw exceptions, terminate() is called.
abort() terminates a program immediately with no clean up.

None of these functions destroys local objects because no stack unwinding occurs. To ensure that the destructors of all local objects are called, you should use exceptions or the ordinary return mechanism to return to and exit main().

p82

This is because it might have better performance than the postincrement operator. The latter involves a temporary object because it must return the old position of the iterator. For this reason, it generally is best to prefer ++pos over pos++.

p108

Calling remove() for elements of a list is a good example of this. If you call remove() for elements of a list, the algorithm doesn't know that it is operating on a list. Thus, it does what it does for any container: It reorders the elements by changing their values. If, for example, it removes the first element, all the following elements are assigned to their previous elements. This behavior contradicts the main advantage of lists — the ability to insert, move, and remove elements by modifying the links instead of the values.

p115

Function objects are another example of the power of generic programming and the concept of pure abstraction. You could say that anything that behaves like a function is a function. So, if you define an object that behaves as a function, it can be used as a function.

So, what is the behavior of a function? The answer is: A functional behavior is something that you can call by using parentheses and passing arguments. For example:
function (arg1 ,arg2); //a function call

All you have to do is define operator () with the appropriate parameter
types:
class X {
public:
//define "function call" operator
return-value operator() (arguments) const;
...
};

p124 容器内的元素必须要可复制,可赋值,可销毁的。

可选特性:默认构造函数可到达,用于容器变大时调用;==和<有定义,用于查找和排序。

p129

If you need a container that has a full commit-or-rollback ability, you should use either a list(without calling the sort() and unique() member functions) or an associative container(without calling their multiple-element insert operations). This avoids having to make copies before a modifying operation to ensure that no data gets lost. Note that making copies of a container could be very expensive.

 

抱歉!评论已关闭.