您的位置:首页 > 编程语言 > C语言/C++

<The C++ Programming Language> 读书笔记

2012-02-26 21:29 501 查看
1.2

The most important thing to do when learning C++ is to focus on concepts and not get lost in

language-technical details.

The difference between C and C++ is primarily in the degree of emphasis on types and struc-

ture. C is expressive and permissive. C++ is even more expressive.

1.3

A programming language serves two related purposes: it provides a vehicle for the programmer to

specify actions to be executed, and it provides a set of concepts for the programmer to use when

thinking about what can be done. The first purpose ideally requires a language that is ‘‘close to the

machine’’ so that all important aspects of a machine are handled simply and efficiently in a way

that is reasonably obvious to the programmer. The C language was primarily designed with this in

mind. The second purpose ideally requires a language that is ‘‘close to the problem to be solved’’

so that the concepts of a solution can be expressed directly and concisely. The facilities added to C

to create C++ were primarily designed with this in mind.

C++ was designed primarily so that my friends and I would not have to program in assembler,C, or various modern high-level languages.

****3.11 Advice

enum

The

range of an enumeration holds all the enumeration’s enumerator values rounded up to the nearest

larger binary power minus 1 1. The range goes down to 0 0 if the smallest enumerator is non-negative

and to the nearest lesser negative binary power if the smallest enumerator is negative. T

P100==A string literal is statically allocated so that it is safe to return one from a function.

----------------------

5.3 In C++, pointers and arrays are closely related.

Functions using void* pointers typically exist at the very lowest level of the system, where real

hardware resources are manipulated. Occurrences of void*s at higher levels of the system should be viewed with suspicion because they

are likely indicators of design errors.

P103:关于struct的对齐:You can minimize wasted space by simply ordering members by size (largest member first).

The name of a structure type can be used before the type is defined as long as that use does not require the name of a member or the size of the structure to be known.However, many such declarations cannot be used unless the type S is defined:(所以支持 class A;声明,但不能定义对象。)

书中的lvalue is an expression denoting a nonconstant object.

P120:操作符的英文释义

Unary operators and assignment operators are right-associative; all others are left-associative.

In particular, underflow, overflow, and division by zero do not throw standard excep-

tions.

Whenever

possible, use standard library facilities in preference to fiddling with pointers and bytes. Standard

library functions may be inlined (§7.1.1) or even implemented using specialized machine instructions. Therefore, you should measure carefully before believing that some piece of hand-crafted code outperforms library functions.

A function that is the best match for one argument and a better than or equal match for all other arguments is called. If no such function exists, the call is rejected as ambiguous.(重载函数的选择问题)

Default arguments may be provided for trailing arguments only.(默认参数只能给尾部)

7.6变长函数参数问题

The first rule about macros is: Don’t use them unless you have to. Almost every macro demonstrates a flaw in the programming language, in the program, or in the programmer. Because they rearrange the program text before the compiler proper sees it, macros are
also a major problem for many programming tools.

7.9 Advice...

Consequently, if a function isn’t found in the context of its use, we look in the namespaces of its arguments.

Overloading works across namespaces. This is essential to allow us to migrate existing libraries to use namespaces with minimal source code changes.

Note that a variable defined without an initializer in the global or a namespace scope is initialized by default. This is not the case for local variables (§4.9.5, §10.4.2) or objects created on the free store.

An inline function (§7.1.1, §10.2.9) must be defined – by identical definitions (§9.2.3) – in every translation unit in which it is used.

Avoid non-inline function definitions in headers; §9.2.1(Linkage) It may seem extravagant to recompile a file each time it is included somewhere, but the included files typically contain only declarations and not code needing extensive analysis by the compiler.

A const member function can be invoked for both const and non-const objects, whereas a non-const member function can be invoked only for non-const objects.

The const makes it clear that the user is not supposed to change the value of this. In a const member function of class X X, the type of this is const X X *const to prevent modification of the object itself(see also §5.4.1)

mutable means can never be const.. Declaring members mutalbe is most appropriate when (only) part of a representation is allowed to change.

!!A member function defined within the class definition – rather than simply declared there – is taken to be an inline member function. That is, in-class definition of member functions is for small, frequently-used functions.

The members’ constructors are called before the body of the containing class’ own constructor is executed. The constructors are called in the order in which they are declared in the class rather than the order in which they appear in the initializer list.

尽量使用成员的初始化列表来初始化(这里默认调用拷贝构造)

Member Constants (10.4.6.2)

The constructor for a local static object is called the first time the thread of control passes through the object’s definition.

The following operators cannot be defined by a user::: (scope resolution; §4.9.4, §10.2.4), . (member selection; §5.7), and .* (member selection through pointer to function; §15.5). They take a name, rather than a value, as their second operand and provide
the primary means ofreferring to members. Allowing them to be overloaded would lead to subtleties [Stroustrup,1994].

A unary operator, whether prefix or postfix, can be defined by either a nonstatic member function taking no arguments or a nonmember function taking one argument. (一个一元操作符,不管是前缀还是后缀的,都可以被定义为一个无参的非静态成员函数或者仅有一个参数的非成员函数)

In particular,operator=, operator[], operator(), and operator-> must be nonstatic member functions; this ensures that their first operands will be lvalues.

An operator function must either be a member or take at least one argument of a user-defined type (functions redefining the new and delete operators need not).

I prefer to minimize the number of functions that directly manipulate the representation of an object. This can be achieved by defining only operators that inherently modify the value of their first argument, such as +=, in the class itself. Operators that
simply produce a new value based on the values of its arguments, such as +, are then defined outside the class and use the essential operators in their implementation:

User-defined conversions are considered only if they are necessary to resolve a call. 当有多个函数定义(模糊)时,编译器更倾向于standard conversion,然后是user-defined conversion。

Copying the return value is often cheaper (in execution time, code space, and data space) than allocating and (eventually) deallocating an object on the free store. I

11.10 Dereferencing 解引用 -> ,重载是被认作是unary postfix operator。

重载++操作符:This int is never used; the argument is simply a dummy used to distinguish between prefix and postfix application.(后缀++要用个无用的int参数)

To get polymorphic behavior in C++, the member functions called must be virtual and objects must be manipulated through pointers or references.

A pure virtual function that is not defined in a derived class remains a pure virtual function, so the derived class is also an abstract class.

All specializations of a template must be declared in the same namespace as the template itself.

the template<> prefix says that this is a specialization that can be specified without a template parameter.

------------

13.

Note that template<class C> says that C is a type name; it need not be the name of a class.

An integer template argument must be a constant. (template<class T, int i> class Buffer...则模版实例化时,i值要为常量)

13.6

Deriving a template class from a non-template class is a way of providing a common implemen-tation for a set of templates. template<class T> class list<T*>: private list<void*>{...};

To distinguish them, what virtual functions provide is called run-time polymorphism , and what templates offer is called compile-time polymorphism or parametric polymorphism.

If no hierarchical relationship is required between these objects, they are best used as template arguments. If the actual types of these objects cannot be known at compile-time, they are best represented as classes derived from a common abstract class.

A member template cannot be virtual. 当前的编译器都期望在处理类的定义的时候就能确定这个类的虚函数表的大小,如果允许有类的虚成员模板函数,那么就必须要求编译器提前知道程序中所有对该类的该虚成员模板函数的调用,而这是不可行的。

15.2 子类中显式使用using声明,可以用于子类和父类同名函数的重载(根据不同的参数,如果参数相同,子类还是会覆盖父类的)

虚基类: virtual base class

The purpose of dynamic_cast is to deal with the case in which the correctness of the conversion

cannot be determined by the compiler.(失败时会返回0值) Dynamic_cast requires a pointer or a reference to a polymorphic type to do a downcast or a crosscast. (只能对指针或引用使用,只能对含有虚函数的类。与 typeid 相比,dynamic_cast 不是一个常量时间的操作。为了确定是否能完成强制类型转换,dynamic_cast`必须在运行时进行一些转换细节操作。因此在使用
dynamic_cast 操作时,应该权衡对性能的影响)

If the operand of a dynamic_cast to a reference isn’t of the expected type, a bad_cast exception is thrown.

RTTI用到的关于typeid和type_info类(百度百科 RTTI)

15.6 Free Store(if you want to supply an allocator/deallocator pair that works correctly for derived classes, you must either supply a virtual destructor in the base class or refrain from using the size_t argument in the deallocator. )

Virtual Constructor:To construct an object, a constructor needs the exact type of the object it is to create. Conse-quently, a constructor cannot be virtual.

16.1 不要试图修改标准库的头文件

The C++ standard library containers were designed to meet two criteria(原则): to provide the maximum freedom in the design of an individual container, while at the same time allowing containers to present a common interface to users.

P440: STL优缺点总结

19.

An iterator is an abstraction of the notion of a pointer to an element of a sequence. (迭代器是指向一序列元素的指针的一种抽象。不是指针,要判断是否为空,只需于end比较)

allocator(分配器,STL中用来将算法和容器的实现隔离于物理存储)

20.

string里的npos默认值被初始化为最大的可能值,用于表示所有成员。(npos is a static member constant value with the greatest possible value for an element of type size_t.)

标准IO操作符(操作IO)

试试valarray,高速计算

一些数学运算的操作(暂时不用)

==========

分析/设计/实现,三部分都不能忽略。有清晰的设计目标

-----------------

23.

开发过程的三个阶段:

分析:定义需要解决问题的范围

设计:建立系统的整体结构

实现:写代码,测试
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: