A Brief Look at C + + Chinese version __c++

Source: Internet
Author: User

A Brief Look at C + + Chinese version <?xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/>

Bjarne Stroustrup

At&t Bell Laboratories

Murray Hill, New Jersey 07974

[Original author]:bjarne Stroustrup (designer of C + +)

[Translator]:kingofark

[Kingofark words]: This article is certainly not new. But I think it's not a bad thing to read as much as possible of the Masters ' thoughts. So it's hard to translate it (Bjarne Stroustrup Master's English article is really not easy to read.) ), trying to intercept a hint of happiness.

Pick to be

This article has focused on answering two questions: what is C + +. How C + + has evolved over the past several years. Experienced C + + users should treat C + + as a practical tool. This article does not intend to compare C + + with other languages, but I answered some specific questions that are often asked by Lisp programmers.

1. introduce

Like all existing programming languages, C + + has evolved and changed over the past several years. For me, the growth rate of C + + is very slow, but the inevitable development, it embodies the internal mechanism of C + + itself in the natural evolution of the use of thousands of C + + users of the experience of a thoughtful response. For many people actively using C + +, this development is disappointingly slow and frustrating. For some people who do not use C + + frequently, this development seems to be an unpredictable behavior, dangling into the unknown. For others, C + + is just an ethereal, little-known thing, and is surrounded by a lot of rumors.

No matter what you think of C + +, it has developed quite a lot compared with the first time it appeared. Show a hand example: a simple function whose function is to sort a container and compute the number of elements 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 viewed as a collection of elements from begin () to end (). In a container, iterator is used 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 literals, the template function can work as described above. For example:

Vector v; C-style string vector

List lst; String list in C + +

// ...

int i1 = CNT (v);

int i2 = CNT (LST);

Vector, list, and string are part of the standard C + + library.

Naturally, we don't have to create string values such as ' Dahl ' and ' Nygaard ' in such a small function. In fact, for any type of container, we can generate a common function that allows any type of value to be manipulated in a range.

Obviously, the programming style here is much different from the traditional C programming style. However, C + + has not lost some of the important virtues of C: adaptability and efficiency. For example, the algorithm sort () function in the C + + standard library used in the above code is several times faster than the Qsort () function in the C standard library in many relatively simple but practical cases.

2. C + + standard

C + + is a kind of static type mechanism (statically-typed), universal purpose (general-purpose) language, which supports object-oriented programming through class mechanism and virtual function mechanism, and supports paradigm programming by template mechanism. and to support the detailed system program design by providing the underlying language facilities. This basic definition sounds pretty good. Although I do not think that the aspects described in the definition can be proved in a strict sense, but I have seen enough facts to make me satisfied with the rationality of this definition, which includes enough good C + + code and enough successful large-scale projects using C + +.

By the year 1989, many factors had made the standardization of C + + inevitable, and the factors considered included the number of C + + users, independent C + + implementations, and the number of providers of C + + tools. If you do not do standardization work, C + + will become a "dialect." By the year 1995, the ANSI and ISO C + + standards committees have reached a level of stability in terms of the basic language structure, the characteristics of the standard library and the level of accuracy in the description, which has led to the release of the C + + standard draft [koenig,1995]. Formal standards will be issued at the end of 1996 or early 1997.

In the process of standardization work, there are many features and libraries have been added to C + +. In general, standardization work confirms and enhances the most basic features of C + + to make it more complete. For some of the new features in C + + and the reasons for these features, you can find the relevant narration in [stroustrup;1994]. Discussion of some of the original features of C + +, as well as discussion of some features that are not yet included in C + + but are considered, can also be found in [stroustrup;1994].

2. 1 language features

Standard C + + is basically the language described in the C + + programming Language (2nd Edition) [stroustrup,1991], plus namespace (namespace), runtime type (run-time Type) information and a few other minor new features. In those minor improvements, the most obvious is the improvement of the template mechanism.

Here 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; that is to say, the shape type is just an interface, not a concrete implementation. We can define a specific type based on this interface. For example, the following code defines the circle type, which is one of shape:

Class Circle:public Shape {

Point Center;

int radius;

Public

Circle (point, int); Constructors (constructor)

void Draw ();

void rotate (int) {}

// ...

};

This allows us to manipulate a variety of shape objects through a common interface. For example, the following function rotates any shape-type vector to an angle of R:

void Rotate_all (vector& v, int R)

{

for (int i = 0; Irotate (r);

}

For each particular Shape object, its corresponding rotate () function is invoked. For example, if the circle object is rotated, then circle::rotate () is invoked.

Consider the following code, which reads shapes from the stream:

void User (istream& ss)

{

io_obj* p = get_obj (ss); Reading objects from the stream

if (shape* sp = dynamic_cast (p)) {//Is a shape type?

Sp->draw (); Working with Shape objects

// ...

}

else//Oh OU: not shape type

Throw Unexpected_shape ();

}

Here, the dynamic_cast operator is used to check whether the object is indeed a shape type. Any kind of shape--, such as circle--, is OK. If you encounter an object that is not a shape, throw an exception.

Of course, the above example has no practical significance. However, the technology involved and the language features that support these technologies have been used in some of the biggest and most difficult application developments ever.

2. 2 Standard library

The lack of a perfect standard library has always been one of the biggest weaknesses of C + +. This flaw not only led to a variety of incompatible "basic library (Foundation Libraries)" of the rapid emergence, but also made the new C + + programmers have to design their own basic library facilities, so far from the goal of accomplishing the task more and more. The latter is particularly bad because it is far more difficult to design and implement an excellent basic library facility than to use it. The lack of a standard library allows many programmers to face advanced C + + features before they have complete knowledge of the basics of C + +.

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

[1] Basic Run-time language Support (Run-time language support) (such as memory allocation, run-time type identification, etc.);

[2] Standard C

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.