A Brief Look AT C ++ Chinese version

zhaozj2021-02-08  631

A Brief Look AT C Chinese version

Bjarne Stroustrup

AT & T Bell Laboratories

Murray Hill, New Jersey 07974

[Original author]: Bjarne Stroustrup (C designer)

[Translator]: KINGOFARK

[Kingofark's words]: This article is not new. But I think it is not a bad thing as possible to read the eldest guides. It is therefore still a laborious translation (the English article of the Bjarne Stroustrup master is really less easy to read!), Trying to intercept a happiness.

Summary

This article has a focus on the two questions: What is C ? How is C evolved in the past few years? Experienced C users should treat C as a utility. This article is not intended to compare C with other languages, but I still answered some specific issues, which are often asked by the Lisp programmers.

1. Introduction

Like all existing programming languages, C is constantly developing and changing in the past few years. For me, the development speed of C is extremely slow; but the development is inevitable, it reflects that C itself has natural evolution in the internal mechanism, and is a well-thoughtable response to the use of thousands of C users. This development is slow and disappointing for many people who actively use C . For some people who don't often use C , this development seems to be a unpredictable behavior, swaying, sliding into unknown. For others, C is just a kind of virtual and unfortunate, and there are few things to know, and accompanied by extreme rumors.

No matter how you look at C , it has been a considerable development than the first time. To make an example to explain: A simple function, its function is to sort a Container and calculate the number of elements between the rows between DAHL and Nygaard:

Template int CNT (C & V)

{

sort (c.begin (), v.end ());

C :: Iterator D = Find (v.begin (), v.end (), "dahl");

Return Count (D, Find (D, V.End (), "Nygaard");

}

A Container can be seen as a collection of elements from begin () to end (). In a Container, use Iterator to identify each element.

As long as Container follows the general principles of the C standard library, and its elements can be compared to String Litrals, then this template function can be operated normally as described above. E.g:

Vector v; // C style string vector

List LST; // C String List

// ...

INT I1 = CNT (V);

INT I2 = CNT (LST);

Among them, VECTOR, LIST and STRING are part of the standard C library.

Naturally, we don't have to build a string value such as 'dahl' and 'Nygaard' in such a small function. In fact, for any type of Container, we can generate a universal function that allows it to operate any type of value in a certain range. Obviously, the programming style here is far from traditional C programming. However, C does not have some important morality: adaptability and efficiency. For example, an algorithm sort () function in the C standard library used in the above code, in many relatively simple but actual examples, several times more than the QSort () function in the C standard library.

2. C standard

C is a language that uses a static-type, general-purpose, which supports object-oriented programming by class mechanism and virtual function mechanism, supporting the model program design by template mechanism. Detailed system programming is supported by providing the underlying language facility. This basic definition is quite good. Although I don't think the aspects described in the definition can be proven in strict sense, I have seen enough facts to make me feel satisfied with this definition, including enough excellent C code And the use of sufficient, successful large-scale projects using C .

In 1989, a variety of factors make C standardization work into an inevitable thing. The factors taken into account include the number of C users, the number of suppliers of C tools. And if you do not standardize, C will evolve into a "dialect". In 1995, the ANSI and ISO C Standards Committee have reached a certain level of stability in the three aspects of the basic language structure, standard library, and the precision level of the description, thus making C standards The draft is released [Koenig, 1995]. The formal standard will be released at the end of 1996 or early 1997.

In the process of standardization, there are many features and libraries to be added to C . In general, standardized work confirmation and enhance C most basic qualities, making it more complete. Relevant descriptions can be found in [Stroustrup; 1994] for some new features in C and the cause of these features. Discussions on C some original features and some discussions that have not been added to C but considering the characteristics, or in [Stroustrup; 1994].

2.1 Language features

Standard C is basically the language described in "THE C Programming" [Stroustrup, 1991], plus the namespace, run-time type information, and several other Secondary new features consist. In those smaller improvements, the most obvious is the improvement of the template mechanism.

Below is a classic example of object-oriented programming with C :

Class shape {

Virtual void Draw () = 0:

Virtual void rotate (int) = 0;

// ...

}

Shape is an abstract class; this means that the Shape type is just an interface without a specific implementation. We can define specific types based on this interface. For example, the following code defines a Circle type, which is a shape: Class Circle: Public Shape {

Point center;

int RADIUS;

PUBLIC:

Circle (Point, int); // Constructor (Constructor)

Void Draw ();

Void rotate (int) {}

// ...

}

This way we can operate a variety of Shape types through a universal interface. For example, the following function rotates any SHAPE type vector:

Void Rotate_all (Vector & V, INT R)

{

For (int i = 0; i rotate (r);

}

For each specific Shape type object, its corresponding rotate () function is called. For example, if the Circle object is rotated, then circle :: rotate () will be called.

Consider the following code, it reads Shapes from the stream:

Void User (ISTREAM & SS)

{

IO_OBJ * P = GET_OBJ (SS); // Read objects from the stream

IF (Shape * SP = DYNAMIC_CAST (P)) {// Is the shape type?

SP-> DRAW (); // Use Shape type object

// ...

}

Else // 欧: Not shape type

Throw unnexpected_shape ();

}

Here, the Dynamic_cast operator is used to check if the object is indeed Shape type. Any shape, such as Circle, is available. If you touch the object is not a shape, you will throw an exception.

Of course, there is no practical meaning in the example. However, the techniques contained in the techniques and supports these technologies have been used in some of the largest and most difficult applications in history.

2.2 Standard Library

Lack of a complete standard library has always been one of C maximum weaknesses. This shortage not only causes the rapid appearance of various incompatible "Foundation Libraries", but also makes the C programmers who have just entered the row to design basic library facilities, thus getting farther and farther away from completing the task. The latter case is particularly harsh, because it is designed and implemented a good basic library facility is much more than using a difficulty. The lack of standard libraries makes many programmers who are forced to face some advanced C features before fully mastering basic C knowledge.

The facilities provided by the standard library can be divided into the following categories;

[1] For basic runtime language support (such as memory allocation, runtime identification, etc.);

[2] Standard C library (in order to minimize the conflict between the type system, there are some minimum modifications);

[3] Strings and input and output flows (to support international character sets and localization);

[4] A Container's Framework (such as Vector, List, etc.) and algorithms that use these Container (such as universal traversal, sorting and combining algorithms);

[5] Support for digital computing (multiple and vector arithmetic operation; class BLAS, universal SLICES; conducive to optimization processing; etc.). The main principles containing a class included in the library: This class should be used almost all C programmers (no doors or experts); this class should be provided in a common form, but with the same functionality Compared to the simple form, it will not bring obvious loads; its basic usage is easy to learn. In general, the C standard library provides the most commonly used basic data structure and the basic algorithm for using these data structures.

For algorithms in the library, any Container does not need to use a conversion operation in terms of a type or the like. This framework - is usually referred to as STL - with scalability, allowing users to easily add their own Container and algorithms on the basis of the Container and algorithms provided by the standard library, and make it directly with the standard Container And the algorithm is used together.

3. Tools, Environment and Library

The stability of all aspects brought about after standardization work has made people surged in the programming environment, library and tools. In the C field, traditional work goals are generally to create such a C language, making: Even in the absence of advanced tools and environments, people can also use C in a major industrial project. This does not hinder the emergence of excellent C tools and the environment, but the evolution of the language itself has had a huge impact on the implementation of the language and the manufacturing of the toolset.

I am looking forward to the real outstanding program development environment being fully applied in the next few years. Many of them have been implemented for C for C that it is impossible to achieve it. It has been achieved in certain commercial products. For example, Sun's C implementation allows you to stop the execution of the program at a BreakPoint, rewrite a function and re-execute the new function. This feature has been in the interpreted language with dynamically-type mechanisms for decades. However, this is an interesting and good development, and it takes another step away from the goals expecting the program development environment. In my goals, the program development environment should be equipped with a static-type language powerful function, and the advantages brought about by complex environments. And such a complex environment should be similar to the dynamically type-based (Dynamical-type) language environment.

The benefits of a huge C user group are the emergence of a large number of libraries. There have been countless C libraries, but the differences between the compilers and the lack of standards have seriously affected the development of the library. The previous problem caused the unnecessary segmentation of the library and causes some libraries that dedicated to addressing cross-platform issues. The latter problem forces the developers to repeatedly designed basic facilities such as String and List. Although it still takes more time to solve these problems from the C system, we now have an alternative, so we can make our minds more important and more interesting.

Automatic Garbage Collection may not be in the two languages ​​of C and Lisp. Lisp language persists: Memory management is too important, so you must not be managed by users. And C language persists: Memory management is too important, so you must not allow the system to manage. C takes a compromise. C allows the use of garbage to collect mechanisms, but it does not force it. From a traditional point of view, this means that C does not use garbage automatic collection mechanism, but now there is a practical use of the C implementation of the Garbage Collector, whether it is commercial or free. The good performance of these garbage collectors is worth looking forward to, especially the pessimism predictions that I have heard it in the years. Even when using the garbage collector, the troublesome C programs are more than the troubles of memory management than the traditional C procedure. In C , memory management is typically packaged in a user-defined type, and the user does not need to distribute and match the memory directly. Especially standard Container such as String, Vector, and List has its own memory management mechanism and provides a variable size data structure. 4. Programming style

C is a language that supports a variety of paradigm. In other words, the C language is designed to support a variety of programming style. No language can support all kinds of programming style at the same time. However, in a language of Framework, you can support a variety of styles. In doing so, we can get quite rich benefits by sharing a general-purpose system and general tool set. These technical advantages can be embodied in some important actual operations, such as providing Groups to meet the various needs of the same needs when sharing a language, which is specific to achieving several specific specifications. The language is much more cost-effective.

For C newbies, C also supports traditional C programming styles. Other programming styles emphasize mechanisms such as classes, abstractions, classes, and templates, in order to be directly, clear, concrete, and relationships between various concepts and concepts. For example, in Section 1 of this article uses a model program design technology, Section 2.1 demonstrates the use of abstract classes and class levels.

In C programming style (such as [Kornig, 1995B] book) and design modes (such as [Gamma, 1994] books, many of the tasks that are looking for a way, to express from various languages ​​and The viewpoint (IDEA) in the system allows those C programmers who write large systems to use these views efficiently and high efficiency. The key is to efficiently use this static type system that is adaptable and scalable in C .

5. Thank you

Thank Craig Knoblock invites me to write this article.

6. reference

[Gamma, 1994] Gamma, Et.al .: Design Patterns. Addison Wesley. 1994. ISBN 0-201-63361-2.

[Koenig, 1995] Andrew Koenig (Editor): The Working Papers for the ANSI-X3J16 / ISO-SC22-WG21 C Standards Committee.

[Koenig, 1995b] Andrew Koenig and Bjarne Stroustrup: Foundations for Native C Styles Software- Practice & Experience 1995. [Stepanov, 1994] Alexander Stepanov and Meng Lee:... The Standard Template Library ISO Programming language C project Doc No.: X3J16 / 94-0095, WG21 / N0482.

[Stroustrup, 1991] Bjarne Stroustrup: The C Programming Language (2nd Edition) Addison Wesley, ISBN 0-201-53992-6. June 1991.

[Stroustrup, 1994] Bjarne Stroustrup: The Design and Evolution of C Addison Wesley, ISBN 0-201-54330-3. March 1994.

转载请注明原文地址:https://www.9cbs.com/read-405.html

New Post(0)