This is the traditional translation of "C frequently asked Questions" found from Taiwan http://www.cis.nctu.edu.tw/chinese/doc/research/, found "C Frequently Asked Questions" Ye Bingzhe, is also a translator of "C Programming Language" 3 / e, this article is very good, paying it for learning purposes, I didn't get the author's authorization, the copyright of the original article still belongs to the original author .c Quotation FAQ Q16: What is the internal function? The inline function is a function that is the program code that is located in the caller. Like the macro, the internal function is exempted from the additional burden of the function call, and (especially!) Can be optimized to it (the program combines "Procedural Integration". However, the macro is different: it will only see the value of all the altends (on the language, "function call" and normal functions, just relatively fast) to avoid some unrecognizable macro errors. In addition, it also detects the type of variable, doing the necessary type of type (macro deleterious; unless absolutely necessary, don't use it again). Note: Excessive use of the line functions will make the program code obesity, in the paging environment, there is a negative performance impact. Declaration: Use "inline" keywords at the function definition: Inline Void F (INT I, CHAR C) {/*...*/} or will be defined in the category: class fred {public: void f (INT I, CHAR C) {/*...*/}}; or outside the category, "inline" is defined: class fred {public: Void F (INT I, CHAR C);} Inline void fred :: f (INT I, CHAR C) {/*...*/} ============================ == ■ □ Section 5: Construction sub-and decomposers ====================================== q17: Constructor is What do you do? The constructing is used to build an object from zero. Constructing a "initialization function"; it makes a bunch of bytes into a living object. Minimize it initializes the internally used field elements, and may also configure the resources required (memory, profile, semaphore, socket, etc.). "CTOR" is the most common abbreviation for constructing subcutor.
=============================================================================================================================================================================================================================================================== q Is the constructive of the same room? There is no way. The reason is: If you call another constructive, the compiler will initialize a temporary regional object; but does not initialize "this" you want. You can use the default parameter to consolidate the two builtons, or share their program code in the private "init ()" member function. ======================================================= DESTRUCTOR? of? The deconstruction is the funeral of the object. The deconstruction is used to release the resources configured by the object, such as the Lock category might lock a semaphore, and the deconstruction is used to release it. The most common example is: After the construction of the construct, the deconstruction is "delete". The deconstruction is a "Method", usually abbreviated as "DTOR". ========================== □ Section 6: Yun operand multi-load ============== =========== Q20: What is Operator OverLoading? It allows people who use categories to operate in intuition. The operation of the operation is multi-loaded to make C / C operand the number of operations (object categories) to self-reserve.
They are similar to the syntactic sugar: class fred {public: // ...}; #if 0fred add (fred, fred); // No way to operate multi-load Fred Mul (Fred, Fred ); # elsefred Operator (Fred, Fred); // has an operational operand multi-load Fred Operator * (Fred, Fred); # endiff f (Fred A, Fred B, Fred C) {# i 0return add (add (mul (A, B), MUL (B, C)), MUL (C, A)); // Nothing ... # Elsereturn A * B B * C C * a; // has ... # Endif } ============================================================================================================================================================================ Multiple load? Most of them can be multi. The number of cirked cystects is "" "and"?: "(And technically, it can be" SIZEOF "). C adds some of its own operations, which can be multi-load except "::" and ". *". Under the bottom, it is an example of the number of operations (it will return a reference). The first is "unused" multi-loaded: Class Array {public: #if 0int & elem (unsigned i) {if (i> 99) error (); return data [i];} # elseint & operator [] (unsigned i) {IF (i> 99) error (); return data [i];} #ENDIF private: int DATA [100];}; main () {Array A; #if 0a.lem (10) = 42; a. ELEM (12) = a.Elem (13); # elsea [10] = 42; A [12] = a [13]; # endif} =============== ================================== Q22: How to do a "**" "secondary" operation number? No solution. The name, priority, combined law, and magnitude of the operand are killed by the language. There is no "**" operation in C , so you can't order one by category. If you are doubt, consider see "x ** y" and "x * (* y)", both of which are exactly the same (in other words, the compiler assumes "Y" is a pointer). In addition, the operation of the operation is only the syntax coating of the function call, although it is sweet, it does not add something.
I suggest you multi-load "POW (Base, Exponent" function (which is the accuracy version in
Member functions and partner functions have equal access privileges (100% right), the main difference is that the partner function is used as "F (x)", and the member function is "X.f ()". Therefore, the partner function allows object categories designers to choose his most pleasant grammar to reduce maintenance costs. The main drawback of partner functions is that when you want to do dynamic binding, it requires additional program code. Want to do the effect of "virtual partners", the partner function should call a hidden (usually in "protected:" mile) virtual member function; like this: "Void F (Base & b) {b.do_f () } ". The derivative category covers the hidden member function ("Void Derived :: DO_F ()") instead of the partner function. ================================================= q26: "Partnership has no inheritance and transfer What does sex? The privilege of partnership cannot be inherited: the partner's derivative category is not necessarily partners (I will treat you as a friend, but this doesn't mean that I will definitely trust your child). If the "Base" category declares "F ()" for its partner, "f ()" does not automatically have special access to some of the "Derived" category derived from "Base". . None of partnership privilege: Partner categories are not necessarily partners for the original category (friends of friends are not necessarily friends). For example, if the "Fred" category declared the "Wilma" category for its partner, and the "Wilma" category declared "F ()" for its partner, "f ()" is not necessarily special for "fred" Access power. =============================================================================================================================================================================================================================================================================================== Or is it partner? Possible words, use member functions; if necessary, use partners. Sometimes in grammar, your partners are better (such as: In the "Fred" category, the partner function can make "FRED" into the second parameter, but in the member function, it must be placed first).
Another good example is: binary middle arithmetic operation operand (such as "Acomplex Acplex" may define a partner rather than a member function, because you want "Afloat Acomplex" to be established; recall A lower: The member function cannot enhance the parameters on the left, because it will change the likelihood that the object belongs to the object of the member function will be changed. In other cases, please select the member function instead of partner functions. ============================================================================================================================================================================================================= == ■ □ Section 8: Input / Output:
C I / O ("<<" and ">>"), and C ("Printf ()" and "scanf ()") compared to: * Type security - to do I / O objects, The compiler will know its model in advance, rather than dynamically governing the "%" column. * Not easy to errors - redundant information will increase the opportunity. C I / O does not require an excess "%". * Faster -Printf is a "interpreter" of a small language, which is mainly composed of "%". In the execution period it uses these fields to select the correct formatting method. The C I / O system is static according to the true type of each variable, selecting a child program to enhance the performance efficiency. * Demonstration - C I / O mechanism can add the new design of the user without changing the original program code (can imagine if everyone is not compatible with "%" fields into Printf And what kind of chaotic scene is it?!). * Subclassable - Ostream and IStream (File * replacement) of Ostream and ISTREAM (substitutes) are true categories, so they can be derived. This means: You can make other custom things have the appearance and behavior of Stream Lei, but actually doing the specific thing you want to do. You automatically reuse the I / O program code written by tens of thousands of others (you don't even know them), and they don't need to know the "extended Stream" category you do. ======================================== q30: Why do I have an input? The end of the file? Because the state of the EOF is to "the action that will exceed the file" will be set. That is, the last byte of the read file does not set the state of the EOF. [Translation] This is also a common error. If your program is like this: INT i = 0; while (! Cin.eof ()) {cin >> x; i; // Work with x} Your I variable will be more than one. What you really do is this: INT i; while (cin >> x) { i; // Work with x} ====================== =================== q31: Why after my program executes the first cycle, will I ignore the requirements for the input? Because the read value is read, the non-digital character is left in the input buffer (INPUT buffer).