B. Stroustrup - The C++ Programming Language (794319), страница 10
Текст из файла (страница 10)
Typically, you can do better without it for many/most classes.[5] Minimize the use of reference and pointer variables: use local and member variables(§3.2.1.2, §5.2, §16.3.4, §17.1).[6] Remember: a variable is never implicitly a reference.[7] Think of pointers as C++’s equivalent to Java references (C++ references are more limited; there is no reseating of C++ references).[8] A function is not virtual by default. Not every class is meant for inheritance.[9] Use abstract classes as interfaces to class hierarchies; avoid ‘‘brittle base classes,’’ that is,base classes with data members.[10] Use scoped resource management (‘‘Resource Acquisition Is Initialization’’; RAII) whenever possible.[11] Use a constructor to establish a class invariant (and throw an exception if it can’t).[12] If a cleanup action is needed when an object is deleted (e.g., goes out of scope), use a destructor for that.
Don’t imitate finally (doing so is more ad hoc and in the longer run farmore work than relying on destructors).[13] Avoid ‘‘naked’’ new and delete; instead, use containers (e.g., vector, string, and map) andhandle classes (e.g., lock and unique_ptr).[14] Use freestanding functions (nonmember functions) to minimize coupling (e.g., see thestandard algorithms), and use namespaces (§2.4.2, Chapter 14) to limit the scope of freestanding functions.[15] Don’t use exception specifications (except noexcept; §13.5.1.1).[16] A C++ nested class does not have access to an object of the enclosing class.[17] C++ offers only the most minimal run-time reflection: dynamic_cast and typeid (Chapter22).
Rely more on compile-time facilities (e.g., compile-time polymorphism; Chapter 27,Chapter 28).Most of this advice applies equally to C# programmers.1.4 HistoryI invented C++, wrote its early definitions, and produced its first implementation. I chose and formulated the design criteria for C++, designed its major language features, developed or helped todevelop many of the early libraries, and was responsible for the processing of extension proposalsin the C++ standards committee.C++ was designed to provide Simula’s facilities for program organization [Dahl,1970][Dahl,1972] together with C’s efficiency and flexibility for systems programming [Kernighan,1978][Kernighan,1988]. Simula is the initial source of C++’s abstraction mechanisms.
The class concept (with derived classes and virtual functions) was borrowed from it. However, templates andexceptions came to C++ later with different sources of inspiration.22Notes to the ReaderChapter 1The evolution of C++ was always in the context of its use. I spent a lot of time listening tousers and seeking out the opinions of experienced programmers.
In particular, my colleagues atAT&T Bell Laboratories were essential for the growth of C++ during its first decade.This section is a brief overview; it does not try to mention every language feature and librarycomponent. Furthermore, it does not go into details. For more information, and in particular formore names of people who contributed, see [Stroustrup,1993], [Stroustrup,2007], and [Stroustrup,1994]. My two papers from the ACM History of Programming Languages conference and myDesign and Evolution of C++ book (known as ‘‘D&E’’) describe the design and evolution of C++in detail and document influences from other programming languages.Most of the documents produced as part of the ISO C++ standards effort are available online[WG21].
In my FAQ, I try to maintain a connection between the standard facilities and the peoplewho proposed and refined those facilities [Stroustrup,2010]. C++ is not the work of a faceless,anonymous committee or of a supposedly omnipotent ‘‘dictator for life’’; it is the work of manydedicated, experienced, hard-working individuals.1.4.1 TimelineThe work that led to C++ started in the fall of 1979 under the name ‘‘C with Classes.’’ Here is asimplified timeline:1979 Work on ‘‘C with Classes’’ started. The initial feature set included classes and derivedclasses, public/private access control, constructors and destructors, and function declarations with argument checking.
The first library supported non-preemptive concurrenttasks and random number generators.1984 ‘‘C with Classes’’ was renamed to C++. By then, C++ had acquired virtual functions,function and operator overloading, references, and the I/O stream and complex numberlibraries.1985 First commercial release of C++ (October 14). The library included I/O streams, complex numbers, and tasks (nonpreemptive scheduling).1985 The C++ Programming Language (‘‘TC++PL,’’ October 14) [Stroustrup,1986].1989 The Annotated C++ Reference Manual (‘‘the ARM’’).1991 The C++ Programming Language, Second Edition [Stroustrup,1991], presenting genericprogramming using templates and error handling based on exceptions (including the‘‘Resource Acquisition Is Initialization’’ general resource management idiom).1997 The C++ Programming Language, Third Edition [Stroustrup,1997] introduced ISO C++,including namespaces, dynamic_cast, and many refinements of templates.
The standardlibrary added the STL framework of generic containers and algorithms.1998 ISO C++ standard.2002 Work on a revised standard, colloquially named C++0x, started.2003 A ‘‘bug fix’’ revision of the ISO C++ standard was issued. A C++ Technical Reportintroduced new standard-library components, such as regular expressions, unordered containers (hash tables), and resource management pointers, which later became part ofC++0x.2006 An ISO C++ Technical Report on Performance was issued to answer questions of cost,predictability, and techniques, mostly related to embedded systems programming.Section 1.4.1Timeline232009 C++0x was feature complete.
It provided uniform initialization, move semantics, variadic template arguments, lambda expressions, type aliases, a memory model suitable forconcurrency, and much more. The standard library added several components, includingthreads, locks, and most of the components from the 2003 Technical Report.2011 ISO C++11 standard was formally approved.2012 The first complete C++11 implementations emerged.2012 Work on future ISO C++ standards (referred to as C++14 and C++17) started.2013 The C++ Programming Language, Fourth Edition introduced C++11.During development, C++11 was known as C++0x. As is not uncommon in large projects, wewere overly optimistic about the completion date.1.4.2 The Early YearsI originally designed and implemented the language because I wanted to distribute the services of aUNIX kernel across multiprocessors and local-area networks (what are now known as multicoresand clusters).
For that, I needed some event-driven simulations for which Simula would have beenideal, except for performance considerations. I also needed to deal directly with hardware and provide high-performance concurrent programming mechanisms for which C would have been ideal,except for its weak support for modularity and type checking. The result of adding Simula-styleclasses to C, ‘‘C with Classes,’’ was used for major projects in which its facilities for writing programs that use minimal time and space were severely tested.
It lacked operator overloading, references, virtual functions, templates, exceptions, and many, many details [Stroustrup,1982]. The firstuse of C++ outside a research organization started in July 1983.The name C++ (pronounced ‘‘see plus plus’’) was coined by Rick Mascitti in the summer of1983 and chosen as the replacement for ‘‘C with Classes’’ by me. The name signifies the evolutionary nature of the changes from C; ‘‘++’’ is the C increment operator. The slightly shorter name‘‘C+’’ is a syntax error; it had also been used as the name of an unrelated language.
Connoisseursof C semantics find C++ inferior to ++C. The language was not called D, because it was an extension of C, because it did not attempt to remedy problems by removing features, and because therealready existed several would-be C successors named D. For yet another interpretation of the nameC++, see the appendix of [Orwell,1949].C++ was designed primarily so that my friends and I would not have to program in assembler,C, or various then-fashionable high-level languages.
Its main purpose was to make writing goodprograms easier and more pleasant for the individual programmer. In the early years, there was noC++ paper design; design, documentation, and implementation went on simultaneously. There wasno ‘‘C++ project’’ either, or a ‘‘C++ design committee.’’ Throughout, C++ evolved to cope withproblems encountered by users and as a result of discussions among my friends, my colleagues,and me.1.4.2.1 Language Features and Library FacilitiesThe very first design of C++ (then called ‘‘C with Classes’’) included function declarations withargument type checking and implicit conversions, classes with the public/private distinction betweenthe interface and the implementation, derived classes, and constructors and destructors. I usedmacros to provide primitive parameterization. This was in use by mid-1980.