TC++PL閱讀筆記

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"

模板

仿函數

Part II Basic Facilities

Part III Abstractions Mechanisms

面向對象編程

泛型編程

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,916評論 0 23
  • 越長大就越懂事,越懂事就越有故事 我一直想做個深有感觸而不發的人,可這個時候想在簡書上發泄發泄 一個女人想做成一件...
    煥語閱讀 439評論 0 0
  • 【引子】 老人在村民們的指責下,氣得沉著臉將白狗拴在石碾子上,隨手從門后操起一根棍子舉著就掄。白狗哦兒哦兒的叫著,...
    民間故事計劃閱讀 2,179評論 0 0
  • 思維是人做事的分析判斷邏輯,講究是非對錯,或者說是哪個更好和哪個更差。 心理是人的心性活動,受意識和潛意識支配,意...
    鶴一張閱讀 245評論 0 0