📄 cpp_faq.txt
字号:
/* ==== CPP_ECHO.FAQ ========================== release 95-08-16 =========This is the Frequently Asked Question list of the fidonet c_plusplus echo.Suggestions, enhancements, changes and comments will be appreciated. _____ Archivist/Editor: Auke Reitsma, Delft, The Netherlands. /_|__| Netmail : 2:281/527/ | \ --------------------------------------------------------------- */This CPP_echo FAQ list currently consists of four sections: "FAQ's", "NewFAQ's", "Abbreviations and Terminology" and "Contributors". Each FAQconsists of a number, the question and a date on a single line -- ifpossible -- the answer, one or more lines listing the contributor and the(last) submission date and a 'last edit date'.The number uniquely identifies the question and will NEVER change. Thequestion date should change when the question is asked again -- or used asanswer -- in the cplusplus echo. In a relatively short time this willidentify the unimportant questions, which will make it easier to deletethese items.Three question marks indicate something that is at least somewhat doubtfuland may need to be changed. Again, contributions are welcome.The "Abbreviations and Terminology" section is included to supply lesscommon abbreviations and terms related to C++ and also to 'explain'abbreviations used in this list. It is also used for indexing and cross-referencing.Auke Reitsma 95-07-15./* ---- Frequently Asked Questions ------------------------------------ */000 What is the topic of the C++ FAQ list? (95-04-15)Any C++ related subject occurring 'frequently' in the cplusplus echo.There must be no doubts _at_all_ that it is on topic in the echo. Thismeans that normal (ANSI) C subjects are not on topic for this list. Alsothe coverage of subjects specific to a platform, compiler or library willbe limited.95-05-01001 What is the difference between C and C++? (95-04-15)The essential difference is the "Object orientation" in C++.95-04-15002 What normal C constructs work differently in C++? (95-04-15)- Assigning int's to enum's.- Assigning void pointers to other types of pointers.- Function declaration foo() without parameters.- Character constants are of type char in C++. They are of type int in C.- ALL functions MUST be prototyped in C++, which is not required in C.- In C++: struct A { /* ... */ }; is equivalent to: typedef struct A { /* ... */ } A; in C.See C++PL2, "Notes to the reader" (p. 11/12) and "C++ and ANSI-C" sectionr.18.2 (p.628/629) for more items and details.Auke Reitsma [95-05-01], Jerry Coffin [95-05-06], David Nugent [95-04-16],Jari Laaksonen [95-04-15], 95-05-16003 Why and when is a virtual destructor needed? (95-04-15)Any class that may act as the base of another class should have a virtualdestructor. This ensures that when an object of the derived class isdestroyed that the derived class dtor will be invoked to destroy it.If the destructor is not virtual, under some common circumstances, onlythe base class' destructor will be invoked, regardless of the classactually being destroyed.For practical purposes this means that a class which does, could or shouldhave virtual member functions, should also have a virtual destructor.Jerry Coffin [95-06-09], 95-06-16004 How do I open binary files? (95-04-15)Yeah, the various manuals and books are usually not clear that subject.With BC and MSC, you do a bitwise or of the file open mode withios::binary, as in: ifstream mystream( "filename.ext", ios::in | ios::binary );The default is ios::text for BC and MSC.However, this is NOT supported by Symantec 6.1. SC 6.1 doesn't have a flagto designate binary - it has ios::translated for text, and apparently towork with binary files you simply designate ios::in or ios::out withoutor'ing in ios::translated.Jerry Coffin [95-05-06], 95-06-16005 Why seem interrupt handlers as member functions to be impossible? (95-04-15)Interrupt handlers as member functions _are_ possible. But they must bestatic member functions. Static member functions don't make use of theimplicit 'this' pointer required by normal member functions. The caller ofthe interrupt handler doesn't know anything about objects and 'this'pointers, so it can't pass a value of such a pointer.Auke Reitsma [95-04-15], 95-04-15006 Is there a new/delete equivalent of realloc? (95-05-01)No. It would have a number of difficult problems. Some of these are:- Should the old instances in the memory be deleted and re-instantiated?- How about contained classes?- How about instances owned by the classes to be reallocated? These often have a pointer to their owner.Origin Unknown [??-??-??], 95-05-01007 Floating point representation and output seems to be compiler dependent. (95-05-01)Regrettably, yes. The action of ios::setprecision() varies among com-pilers.David Nugent [95-04-16], 95-05-01008 What is a "Copy Constructor"? (95-07-16)A Copy Constructor constructs a new object as a copy of an existing objectof the same type. Frequently copy constructors do a 'deep copy' of theobject. X( const X& X_object ){...}; is a copy constructor for class X.Deep Copy vs. Shallow Copy: a shallow copy simply copies the contents ofan object directly - if the object contains pointers, both the old copyand the new copy contain pointers to the same actual item. In a deep copy,when an object contains a pointer, a new copy of whatever the pointerpoints AT is created and the new object contains a pointer to the newlycreated copy of the item.Why are deep copies important? If you carry out a shallow copy you end upwith two pointers to the same item. If that item is an object with adestructor, this generally means you'll end up calling the destructor forthat item twice, which will generally cause problems.Unfortunately, most don't know to ask this question directly: the symptomis generally heap corruption which is hard to track down directly sincethere it has many possible causes.Jerry Coffin [95-07-04], 95-07-16009 Why a "operator=(...)" when there is a copy ctor? (95-07-16)You use the assignment operator (operator = ()) whenever an existingobject is to be replaced with a different object. The copy constructorX(const X&) is used to create a new instance of an X-object exactly likeanother.Notice the subtle difference. Assignment changes an existing object whileconstruction creates a new object. You can view assignment as the applica-tion of a destructor, to flush away the existing object, followed by acopy construction, to make an exact copy of the assigned object.Cliff Rhodes [95-07-08], 95-07-16010 What is an ABC: an "Abstract Base Class"? (95-08-01)An Abstract Base Class is a class that is not intended to be instantiateditself. Rather, it is intended strictly for use as a base for otherclasses. To prevent instantiation, an ABC will typically contain at leastone pure virtual function.The point of an ABC is to separate the interface of a group of classesfrom the implementation of the functions that make up the interface. Thisallows other code to ignore differences in how these functions are carriedout. An ABC creates a contract between its descendants and any other codethat uses them. The descendants must implement a certain set of functions.Code that uses them must use those functions to access whatever it is theobject involved represents.Jerry Coffin [95-07-14], 95-08-01011 How is an ABC related to an "Abstract Data Type" (ADT) (95-08-01)An ADT is a concept - the basic idea of a data type that doesn't specifyhow the data type is implemented. An ABC is a method C++ provides forcreating an ADT.Jerry Coffin [95-07-26], 95-08-01012 What is a 'pure' virtual function and what's its use? (95-08-01)A pure virtual function is signified by using `=0;' in place of the bodyof the function. The presence of a pure virtual function preventsinstantiation of the class which contains it. For this to be of any use, aderived class must implement the pure virtual function. I.e. the derivedclass must provide a function with the same name which includes a functionbody.The basic reason for pure virtual functions is to specify something thata class can do without specifying how the class will do it.Jerry Coffin [95-07-26], 95-08-01/* ---- New since previous release -- provisionally numbered ---------- */None./* ---- Abbreviations and Terminology --------------------------------- */This section also serves as an index.ABC Abstract Base Class, see FAQ #010 and #011ADT Abstract Data Type, see FAQ #011ARM "The Annotated C++ Reference Manual" by B. Stroustrup and M.A. Ellis. ISBN 0-201-51459-1assignment operator See FAQ #009BC Borland C. Usually including the Turbo products.binary files See FAQ #004C++PL2 "The C++ Programming Language" second edition by B. Stroustrup, ISBN 0-201-53992-6copy constructor See FAQ #008ctor constructordeep copy See FAQ #008dtor destructorinterrupt handlers See FAQ #005MSC Microsoft C.pure virtual function See FAQ #012SC Symantec C.shallow copy See FAQ #008static member functions See FAQ #005subclass Not a correct C++ term at all. Use "derived class". See ARM p.197superclass Not a correct C++ term at all. Use "base class". See ARM p.197virtual destructor See FAQ #003virtual function, pure See FAQ #012/* ---- Contributors -------------------------------------------------- */Jerry CoffinJari LaaksonenDavid NugentAuke ReitsmaCliff Rhodes/* ==== CPP_ECHO.FAQ end ============================================== */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -