In the beginning, C++(www.cppentry.com) was just C with some object-oriented features tacked on. Even C++(www.cppentry.com)’s original name, “C with Classes,” reflected this simple heritage.
As the language matured, it grew bolder and more adventurous, adopting ideas, features, and programming strategies different from those of C with Classes. Exceptions required different approaches to structuring functions (see Item 29). Templates gave rise to new ways of thinking about design (see Item 41), and the STL defined an approach to extensibility unlike any most people had ever seen.
Today’s C++(www.cppentry.com) is a multiparadigm programming language, one supporting a combination of procedural, object-oriented, functional, generic, and metaprogramming features. This power and flexibility make C++(www.cppentry.com) a tool without equal, but can also cause some confusion. All the “proper usage” rules seem to have exceptions. How are we to make sense of such a language
无人能掌握C++(www.cppentry.com)所有的枝节。这并非夸张的说法,也不是藐视读者的智商。因为C++(www.cppentry.com)本身不断在发展,不断地加入新的东西。
很多年之前,我学习C++(www.cppentry.com)时用的第一个C++(www.cppentry.com)编译器(Turbo C++(www.cppentry.com) 1.0)中,template还只是一个被保留而未实现任何功能的关键字。可在C++(www.cppentry.com)诞生的若干年后,它居然成为了 STL的基石。这个不起眼的小玩意,即使是C++(www.cppentry.com)之父,一开始也未能意识到其蕴涵的巨大能量。
The easiest way is to view C++(www.cppentry.com) not as a single language but as a federation of related languages. Within a particular sublanguage, the rules tend to be simple, straightforward, and easy to remember. When you move from one sublanguage to another, however, the rules may change. To make sense of C++(www.cppentry.com), you have to recognize its primary sublanguages. Fortunately, there are only four:
C. Way down deep, C++(www.cppentry.com) is still based on C. Blocks, statements, the preprocessor, built-in data types, arrays, pointers, etc., all come from C. In many cases, C++(www.cppentry.com) offers approaches to problems thatare superior to their C counterparts (e.g., see Items 2 (alternativesto the preprocessor) and 13 (using objects to manage resources)),but when you find yourself working with the C part of C++(www.cppentry.com), therules for effective programming reflect C’s more limited scope: notemplates, no exceptions, no overloading, etc.
Object-Oriented C++(www.cppentry.com). This part of C++(www.cppentry.com) is what C with Classes was all about: classes (including constructors and destructors), encapsulation, inheritance, polymorphism, virtual functions (dynamic binding), etc. This is the part of C++(www.cppentry.com) to which the classic rules for object-oriented design most directly apply.