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 FAQ Q94: SmallTalk / C Different inheritance, what is the result caused in reality? SmallTalk lets you make a sub-model that is not subcategory, making a sub-category, which allows the SmallTalk program to put which information (bit, expression, data structure) put into the category ( For example, you might put the link serial in the stack category). After all, if someone wants a stack made in an array, he doesn't have to inherit from the stack; if you like, he can inherit from the array category Array, even if ArrayBasedStack is "not" a group! ) In C , you can't worry about it. Only the mechanism (program code for operation behavior), not the performance method (data bits) can be covered in subcategories, so you usually "don't" put the data structure in the category. This will contribute to the strong use of Abstract Base Classes (Abcs). I like to be metaphor between ATV and MASERATTI. ATV (All Terrain Vehicle, off-road car) is very fun, because you can "get around", freely open to fields, small streams, and sidewalks. On the other hand, Maseratti allows you to travel at high speed, but you can only open it on the road. Even if you like "free expressive", you like to drive to the jungle, but please don't do this in C ; it is not suitable. ======================================================= q95: I learned "purebred" OOPL Can you learn C ? Not (in fact, this may harm you). (Note: SmallTalk is a "pure species" OOPL, while C is a "mixed-borne OOPL.) Before reading this, read the previous FAQS regarding C and SmallTalk. OOPL's "pureness" does not make it easier to transfer to C . In fact, a typical dynamic series and non-non-non-non-non-non-non-non-non-non-non-non-non-non-non-non-non-non-non-non-non-non-non-child.
Paradigm Shift has taught thousands of OO technology, we noticed that people with SmallTalk backgrounds come to learn C , usually and those who have not touched inheritance. In fact, people who have highly experienced OOPL (usually, but not all are smaltalk), it may be "more difficult" to learn well, because I want to learn the past habits "forgotten", will learn more than one Static types are difficult. [Translation] The author is seen in the perspective of "language learning". In fact, if there is a background knowledge of the objects such as SmallTalk, then learn C does not have to convert "paradigm" - the center thinking of the object orientation will not change, and it is only implemented. ========================================================== What is NiHCL? Where can I get it? NIHCL represents "National-Institute-of-Health'S-Class-Library", US National Health Bureau object link library. Take the law: Anonymous FTP to [128.231.128.7], file: Pub / NiHCl-3.0.tar.z. NiHCl (someone thinks "N-I-H-C-L", some people think "nickel") is a C object link library that is transferred by SmallTalk. Some NiHCLs are used to dynamic types (such as: Persistent Objects, persistent object), and some local dynamic doins will conflict with C language static types, resulting in tension. See the FAQs for SmallTalk for details. =============================== ■ □ Section 16: Reference and value language ========= ================================================================================================================================================ Which one is best in C ? In Reference Semantics, "Settings" is a "pointer copy" action (which is the original meaning of "reference"), the value of value semantics, or "copy" semantics) is really true "Copy the value" instead of doing the actions of the pointer copy.
C let you choose: Copy your value with the setting operation operation, or copy the pointer with a pointer copy mode. C allows you to override the Override settings, let it do what you want, but the system preset (and the most common) method is to copy its "value". Reference Survey: Elasticity, Dynamic Dynamic (in C , you can only achieve dynamic series with a password or pass reference, rather than using a pass value). Numerical Surveus: Speed. For the case where the object (rather than pointer) is required, "speed" seems to be very strange, but in fact, we are more often accessible to the object itself, less often copy it. So the cost of the occasional copy, (usually) will have "real object itself", rather than the benefits of pointing to the object to compensate for the past. There are three situations where you will get real objects, not pointing to its pointer: region variable, overall / static variable, fully included in a category member object. The most important thing here is the last one (that is, "ingredients"). The back FAQS will have more information about COPY-VS-REFERENCE, please read it to get a relatively balanced point. A few days ago, I deliberately biased the numerical language, so if you read the front, your point of view will be biased. There are other matters (such as: shallow vs deep copy) not mentioned here. ========================================================================================================================================== q98: "Virtual Data"? How: Why should I use it in C ? The virtual data allows the derivative category to change the category belonging to the subject member of the base category. Strictly speaking, C does not "support" virtual data, but can simulate it. Not beautiful, but it can be used. If you want to simulate, the base category must have a pointer to the member object, the derivative category must provide a "new" object to make the original base category pointer to the pointer. This base category also has more than one normal construction to provide their own reference (also through "new"), and the base category deconstruction is also "delete". Reference. For example, the "Stack" category may have an Array member object (adopt a pointer), the derived category "Stretchablestack" may "Array" "Array", "StretchableArray", which is derived "streetchablestack". If you want to do it, StretchableArray must inherit from Array, so STACK will have an "array *".
The normal construction of Stack will use "New Array" to initialize its "array *", but Stack will also have one (probably "protected:" in) special constructors to receive a self-derived category "Array * "; STRETCHABLEARRAY's constructors will pass it to that special constructive with" New StretchableArray ". Advantages: * Easy to make Stretchablestack (most of the procedures are inherited). * The user can pass the streetchablestack as "is a" "STACK to pass. Disadvantages: * Add additional indirect access layers to touch Array. * Add additional free memory configuration burden (New and Delete). * Add additional dynamic set burden (please see next FAQ). In other words, in the side of "We", it is easy to make STRETCHABLESTACK, but all users have payded at this. Unfortunately, additional loads are not only in Stretchablestack, but even STACK will also. Please see the next FAQ to see how much the user will "pay". Please also read the next few FAQs (don't look at other, you will not get a balanced report). ================================================ q99: What is the difference in virtual data and dynamic data ? The most easily distinguished method is to see quite similar "virtual functions". The virtual member function refers to: In all subcategories, its declaration (type signature) part must remain unchanged, but part of the (body) can be overridden. The inherited member function can be covered, static property; it does not dynamically change the life of any object, different objects of the same subcategory, can also have different member functions . Now, please reread the previous paragraph, but make some exchange: * "Member function" -> "member object" * "Sample sign" -> "Type" * "本体" -> "true belong Category "This is the definition of virtual data. From another perspective, it is to distinguish the member functions of "various objects" from the "Dynamic" member function. "Each object" member function means: In any object case, the member function may vary, may be stuffed into the function pointer to come out; this pointer can be "const" because it is not in the object of the object Will change. The "Dynamic" member function means that the member function changes dynamically over time; it is also possible to do in a function pointer, but the pointer is not Const. Increased, we get three different information members concept: * Virtual data: Definition of member objects (true belonging) can be covered by subcaters, as long as its declaration (model) remains unchanged, and this coverage is Subclassical static properties.
* Objects of each object: Any class of objects can be case different types of members objects (usually a "Wrapper" package), and the active category of the member object is The static properties of the object it wrapped up. * Dynamic data: The active category that the member object is actually changed. They look like, because C does not "directly support" they, just "can do it"; in this case, simulation their mechanism is the same: pointing (possibly abstract) base category index. In the language of these "first Class" abstraction mechanisms directly, the difference between these three is very obvious, and they have different grammar. ============================================================= Data members, or use "Composition"? Ingredients. Under normal circumstances, your member information should be "included" in the synthetic object (but not always the case; "Wrapper" object is a good example you want to use; N-TO-1-Uses- A relationship also requires something such as a pointer / reference. There are three reasons, completely included, the efficiency of the membership ("ingredients"), better than the free-configurable object: * Additional indirect layers, whenever you want to access member objects. * Additional dynamic configuration ("New" is in the construction of the subconstruction, "Delete" is in the deconed man). * Additional dynamic series (explained below). ============================================================331: Dynamic configuration members objects have three efficiency factors What is their relative cost? These three efficiency factors, the last FAQ is listed: * In its own, the additional indirect layer has little effect. * Dynamic configuration may be an efficiency problem (when there are many configuration actions, typical Malloc will slow down slow; OO software will be dynamically configured, unless you pay attention to it in advance). * With pointers instead of objects, additional dynamics will be brought. Whenever the C compiler knows a "real" category, the virtual function call can be "static", which can be in fromline. Inline may have unlimited (but you may only believe that there is a half-style optimization opportunity, like ProceduralIntegration, buffer life, etc. Three cases, C compiler can know the true category of objects: Regional variables, overall / static variables, fully included members.
Completely included member objects can achieve a large optimization effect, this is impossible to do it. This is why it is the reason why the reference to the language will be "freely". Note: Please read the three FAQs below to get a balanced point of view! ====================================================002: "Inline Virtual" member function Will it be "inline"? YES, but a Virtual call through the pointer or reference is always dynamically determined, which may never be inline. Cause: The compiler will only know which program will be known when it is executed (ie: dynamically), because the program may come from the derivative category that appears after the caller compile. Therefore, the Inline Virtual call can be by the unique timing of Inline: The compiler has a way to know when a "true category" is a matter of virtual function call. This only occurs: the compiler has a real object, not the pointer or reference of the object. That is, it is not a zone variable, an overall / static object, is a fully included object in the synthesis object. Note: Inline and non-Inline gaps are usually more remarkable than normal and virtual functions. For example, normal and virtual function calls, usually only two memory references, but inline and non-inline functions will have a magnitude-level gap (compared to member function calls that are not very affected by tens of thousands of impacts) Without inline Virtual, it will cause 25X efficiency losses! [Doug Lea, "Customization IN C ," Proc hasenix C 1990]). For this phenomenon: Do not fall into compiler / language manufacturers, for each other Virtual function call, doing never endless performance compassion (or advertising!). Whether the language / compiler can call the member function called "Introduction to", this is completely meaningless. That is, many language compilers, desperately emphasize how good their function is distributed, but if they don't do "in-line" member function call, the overall performance will still be poor, because inline - rather than distribution It is the most important performance influencing factor. Note: Please read the two FAQs to see another statement! ============================================================================================================================================================================== q103: I shouldn't use the reference language Is it? wrong. Reference language is a good thing. We can't abandon the pointer, we only need to make a pointer to the software into a big mouse land.