Effective C ++ 2e Item2

zhaozj2021-02-11  223

Terms 2: Try to use without

Yes, Scanf and Printf are very light, very efficient, you have long known how to use them, this I admit. But although they are useful, in fact, Scanf and Printf and their series can also do some improvements. In particular, they are not type safe and there is no scalability. Because type security and scalability are the cornerstone of C , you have to obey this. In addition, the Scanf / Printf series function separates the variables to be read and the information on the information on the read / write format, just like ancient Fortran. It is time to say to the fifth year!

Don't be amazed, these weaknesses of Scanf / Printf are operators >> and << strong items: int I; Rational R; // r is a number

...

CIN >> I >> R; COUT << i << r;

The above code must be compiled, >> and << must be a heavy-duty function that can handle the Rational type object (may be converted by implicit type). If such a function is not implemented, it will be wrong (handling Int does not do this because it is a standard usage). In addition, the compiler can select different forms of operators according to different variable types, so it is not necessary to deserve that the first object to be read is Int, and the second is Rational.

In addition, the grammar form is the same as the syntax form used when reading and writing objects, so it is not necessary to remember some of the SCANF, for example, if the pointer is not obtained, addicts, and if the pointer has been, it is necessary to add it. Address. These can be handed over to the C compiler. There is nothing else to do anything else, but you are different. Finally, it is important to note that the custom type like INT and the custom type of Rational Rational is the same. And you will try it with SACNF and Printf!

The code you have written to represent the rational class may like the following: Class Rational {public: Rational (int name = 0, int devenator = 1);

...

Private: int N, d; // molecule, branch

Friend Ostream & Operator << (Ostream & S, Const Rational & R);

Ostream & Operator << (Ostream & S, Const Rational & R) {s << r.N << '/' << r.d; returnide

The above code involves some subtle (but very important) usage of Operator <<, which is discussed in detail in this book. For example: The above Operator << is not a member function (Terms 19 explains why), and it is not a reference to operator <<, but is not a reference to the object of Const (see Terms 22). The statement and implementation of Operator >> are similar.

Although I am not willing to admit it, it is still very meaningful to return to those who have proved and correct old roads. First, some iostream's operations are achieved less than the corresponding C stream efficiency, so different options will give you the program (although not necessarily, see the Terms M16) brings a lot. But please keep in mind that this is not for all Iostream, just some special implementation; see Terms M23. Second, in the standardization process, the iostream library has made a lot of modifications in the underlying (see Terms 49), so for those applications that require maximum portability, different manufacturers will follow the standards. Third, the functionality of the iostream library is constructed and the function in does not, when some sequence involves the initialization of static objects, if it is confirmed that there is no hidden danger, use the standard C library more simple and practical. . The value of the type of security and scalability provided by the Iostream library and functions are far more than your original imagination, so don't just discard it just because you are used to . After all, after the transition to iostream, you will not forget .

By the way, the title of this Territor has not been printed; I do is rather than . It is technically said that there is no this - the Standardization Commission replaces it with when simplifying the non-C standard header file. The reason they do is explained in terms 49. It must also be known that if the compiler supports and , the use of the file name will be very subtle. For example, if #include is used, it is the element of the iostream library placed under the namespace STD (see Terms 28); if #include is used, it is the same as the global space. Elements. Acquiring elements in global space can lead to name conflicts, while the original intention of design name space is used to avoid this conflict in this name. Also, when typing, is more than less words, this is also why many people use it. :)

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

New Post(0)