2017.03
Bjarne Stroustrup. The C++ Programming Language. 4ed, Person Education, Inc. 2013
笔记思路
- 重点摘抄
- 自己的总结
笔记的目的是帮助阅读中的理解和遗忘后回顾,主要是给自己看。
前言部分
本书内容和写作方式
本书厚达一千三百多页,目的是 completeness,用Stroustrup大神的原话:
I describe every language feature and standard-library component that a professional programmer is likely to need. For each, I provide:
- Rationale: What kinds of problems is it designed to help solve? What principles underlie the design? What are the fundamental limitations?
- Sepecification: What is its definition? The level of detail if chosenn for the expert programmer;
- Examples: How cant it be used well by itself and in combination with other features? what are the key techniques and idioms? What are the implications for maintainability and performance?
所以阅读的过程中也要按这个结构去看每一部分内容, 才能理解的更好理快。
C++是一个什么样的语言?
C++ is a general-purpose programming language emphasizing the design and use of type-rich, lightweight abstractions. It is particularly suited for resource-constrained applications, such as those found in software infrastructures.
Part I Introduction
Overview of the major concepts and features and its standard library.
The Design of C++
C++ is based on the idea of providing both
- direct mappings of built-in operations and types to hardware to provide efficient memory use and efficient low-level operations, and
- affordable and flexible abstraction mechanisms to provide user-defined types with the same notational support, range of uses, and performance as built-in types.
The design of C++ has focused on programming techniques dealing with fundamental notions such as memory, mutability, abstraction, resource management, expression of algorithms, error handling, and modularity. Those are the most import concerns of a systems programmer and more generally of programmers of resource-constrained and high-performance systems.
C++ support four programming styles:
- Procedural programming
- Data abstraction: 用接口隐藏细节(abstract class)
- Object-oriented programming: Class hierarchies provide run-time polymorphism and encapsulation.
- Generic programming: Template,
The notion of static types and compile-time type checking is central to effective use of C++. It prevents accidental corruption of data, C++ protects against accident, not against fraud.
相比于C, Dennis Ritchie said "C is a strongly typed, weekly checked language."
Learning C++
Language features exist to support a variety of programming styles and techniques. Consequently, the task of learning a language should focus on mastering the native and natural styles for that language - not on understanding of every little detail of every language feature. Writing programs is essential.
Fundamental application concepts are represented as abstraction(e.g., classes, templates, and class hierarchies) in libraries. Many of the most fundamental programming concepts are represented in the standard library. Thus, learning the standard library is an integral part of learning C++. The standard library is the repository of much hard-earned knowledge of how to use C++ well.
The most important thing to do when learning C++ is to focus on fundamental concepts (such as type safety, resource management, and invariants) and programming techniques (such as resource management using scoped objects and the use of iterators in algorithms) and not get lost in language-technical details.
The question "How does one write good programs in C++?" is very similar to the question "How does one write good English prose?" There are tow answers: "Know what you want to say" and "Practice. Imitate good writing."
Invariants
A statement of what is assumed to be true for a class is called a class invariant, or simply an invariant (e.g. "class member elem points to an array of sz doubles").
The notion of invariants underlies C++'s notions of resource management supported by constructors and destructors.
另外还可以参考 loop invariant 的概念.
简述 C++ 的抽象机制
C++提供了两种抽象机制:“类”和“模板”
类
- 具体类:The basic idea of concrete classes is that they behave "just like built-in types." The defining characteristic of a concrete type is that its representation is part of its definition.
- 抽象类:An abstract type is a type that completely insulates a user from implementation details. A class that provides the interface to a variety of other classes is often called a polymorphic type.
RAII: The technique of acquiring resources in a constructor and releasing them in a destructor, known as Resource Acquisition Is Initialization or RAII, allows us to eliminate "naked new operations"
模板
仿函数