Effective C ++ 2e Item50

zhaozj2021-02-11  206

Terms 50: Improvement to C

There are many "things" in C : C, overload, object-oriented, template, exception, name space. So many things, sometimes people feel unknown. How do you understand all these things?

The reason why C has developed to this look, it has its own design goals. Understand these design goals, it is not difficult to understand all these things. The most important goal of C is:

• Compatibility of C. A lot of many C still exist, many of many C programmers still exist. C uses this foundation and built in ---- I refer to "Balance" ---- this basis.

· effectiveness. As the C designer and the first implementator, Bjarne Stroustrup knows from the beginning, if you want to strive to win the C programmer, avoid the conversion language will bring your performance, otherwise they will not pair C Look at the second eye. As a result, he is convinced that C can be in efficiency and c matching ---- the two is about 5%.

• Compatibility with traditional development tools and environments. Different development environments are everywhere, compilers, linkers, and editors are everywhere. From small to large development environments, C should be easily copied, so the baggage bits are, the better. Want to transplant C ? You actually transplant is just a language and uses existing tools on the target platform. (However, it is often possible to achieve better implementation, for example, if the linker can be modified, it can handle the inline and template higher requirements in some respects)

· Available in real problems. C is not designed as a perfect, pure language, is not suitable for teaching students how to program. It is a powerful tool designed as a professional programmer to use it to solve the true problems in various fields. The real world has some bumps, so the tools that the programmers can rely on occasionally a problem, and it is not worth the strange.

The above objectives illustrate a lot of implementation details in the C language. If they do not have their guidance, there will be friction and confusion. Why implicitly generated copy constructors and assignment operators should work like this, especially pointers (see clauses 11 and 45)? Because this is C to copy and assign values ​​for Struct, and C is important. Why is the destructor not automatically declared as Virtual (see Terms 14), why do you implement detail in the definition of class (see Terms 34)? Because it doesn't do this, it will bring performance, and efficiency is very important. Why can't C cannot detect initialization dependencies between non-local static objects (see Terms 47)? Because C supports individual compilation (ie, the translation source module is separated, then link multiple target files, forming executable programs), relying on existing linkers, not dealing with the program database. Therefore, the C compiler must hardly know everything throughout the program. The last point, why does C will free programmers from some complicated matters such as memory management (see Terms 5-10) and low-level pointer operations? Because some programmers need these processing capabilities, a real programmer needs is critical.

About how the design goals behind C affect the formation of language behavior, the above introduction is not enough. To cover all the content, you will need a whole book; convenient is that Stroustrup wrote a copy. This book is "The Design and Evolution Of C " (Addison-Wesley, 1994), sometimes referred to as "D & E". Read it, you will learn what features have been added to C , in what order, and why. You will also know which features are abandoned, and why. You can even learn how late story, such as Dynamic_cast (see Terms 39 and M2) how to be considered, abandoned, and considering, finally acceptable --- and why. If you understand C is difficult, D & E will be suspicious of you. For C how to become a present, "The Design and Evolution Of C provides a wealth of information and insights, but it is definitely not a formal language specifications. For this, you have to help C international standards, an impressive level of 700 pages of formal text. Where you can read the sentence below:

The default parameter used by a virtual function call is the default parameters declared by the virtual function determined by the pointer to the object or the referenced static type. The overloaded function in the derived class does not get the default value in the function it overload.

This paragraph is the basis of clause 38 ("Never redefine the default parameter value of the inheritance"), but I expect that my discussion of this topic is more likely to understand more than the original text.

C standard is not a casual reading before going to sleep, but your best reliable ---- your "standard" rely --- If you and others (such as compiler supplier, or other tool programming Developers) What is or not C with differences. The entire purpose of the standard is to provide authority information to solve such disputes.

The official name of the C standard is very biting, but if you need to know, you have to know. This is: International Standard for Information Systems ---- Programming Language C . It was promulgated by the INTERNATIONAL Organization for Standardization (ISO) 21st Working Group. (If you love to drill the horns, it is actually promulgated by ISO / IEC JTC1 / SC22 / WG21 - I don't add oil to vinegar) You can start from your national standard (in the United States, it is ANSI, American National Standards Institute) Order a copy of the official C standard, but the latest draft copy of the C standard ---- and the final file is very similar (although not exactly) ---- On the Internet is free. A good place to find it is "The Cygnus Solutions Draft Standard C Page" (http://www.cygnus.com/misc/wp/), the Internet is very fast, if you find that this website is not connected, don't connect strange. If so, search engines will help you find a correct URL.

I said, "The Design and Evolution Of C " is very good for understanding the design ideas of C languages, C standards clarify the specific details of the language; if you are in the "D & E Thousand Miles" and "C Standard Micro" There is a good bridge between the existence. The tutorial should be suitable for this role, but their perspective is often biased towards the standard, more focused on what is language, and not explained why. Enter ARM. ARM is another book, "The Annotated C Reference Manual" (Addison-Wesley, 1990), the author is Margaret Ellis and Bjarne Stroustrup. This book has become an authority of C , and international standards are based on ARM (and existing C standard). In the past few years, the C standards and ARM have differences in certain aspects, so ARM is no longer as authoritative as in the past. But it is still very reference value, because most of it is still correct; so, in the C field, some manufacturers still adhere to the ARM specification, this is quite a few, after all, the standard is only ready recently.

However, making the ARM really useful nothing of the RM portion, but the annotation: comment. A lot of features for C "Why" should work like this, ARM provides a comprehensive explanation. These explanations D & E have some, but most of them don't need to know them. For example, I first encountered the following code, most people will crazy for it:

Class base {public: Virtual void f (int x);

Class Derived: Public Base {public: Virtual Void F (Double * Pd);

Derived * pd = new derived; pd-> f (10); // error!

The problem is that Derived :: f hides Base :: F, even if they take different parameter types; so the compiler requires a Double * to the call to f, and 10 this figure is of course not.

This is not very reasonable, but ARM provides this behavior to understand. Assume that when you call f, you really want to call the version in Derived, but accidentally use the wrong parameter type. Further hypothesis is the lower layer of the inherited hierarchy, you don't know that Derive has indirectly inherits a base class baseclass, and BaseClass declares a virtual function f with an int parameter. In this case, you will invoke Baseclass :: F, one you don't even know the function it exists! In the case of using a large class hierarchy, this error occurs often; so, in order to prevent it, Stroustrup decides to enable the derived class to hide the base class member according to the name.

By the way, if you want Derived users to access Base :: F, you can easily accomplish it through a using declaration:

Class Derived: public base {public: using base :: f; // introduced Base :: F into // derived spatial range Virtual Void F (double * pd);}; derived * pd = new derived; pd-> f (10); / / correct, call Base :: F

Another selection is an inline function:

Class Derived: Public Base {public: Virtual Void f (int x) {base :: f (x);} Virtual void f (double * pd);

Derived * pd = new derived; pd-> f (10); // correct, call derived :: f (int), // Indirect call Base :: f (int)

With D & E and ARM, you will get a thorough understanding of C , so that you may get to: Sometimes it seems that it is a reasonable and serious structural design after the look of the Baroque style. (Translation: The Baroque style building is extremely rich, pink jade, so the structure is complex, even a bit weird) combines these understanding and C standard details, you stand on the solid foundation of software development, thus Towards the truly effective C programming road.

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

New Post(0)