1. To C++ programmer, for example, a pointer to a pointer looks a little funny. Why, we wonder, wasn鈥檛 a reference to a pointer used 聽instead?
聽聽聽聽聽聽 const char chr[] = "chenzhenshi&guohonghua";
聽聽聽聽聽聽 const char*聽 pchr = chr;
聽聽聽聽聽聽 const char** ppchr = &pchr;
聽聽聽聽聽聽 const char*&聽 rpchr聽 = pchr; // a reference to a pointer
聽聽聽聽聽聽 std::cout << pchr << ' ' << *ppchr << ' ' << rpchr << std::endl;
2. C is a fairly simple language. All it really offers is macros, pointers, structs, arrays, and functions. No matter what the problem is, the solution will always boil down to macros, pointers, structs, arrays, and functions. Not so in C++. The macros, pointers, structs, arrays and functions are still there, of course, but so are private and protected members, function overloading, default parameters, constructors and destructors, user-defined operators, inline functions, references, friends, templates, exceptions, namespaces, and more. The design space is much richer in C++ than it is in C: there are just a lot more options to consider.
3. The Item might better be called 鈥減refer the compiler to the preprocessor鈥?
4. 聽聽 const char* pc;
聽聽聽聽聽聽 pc = a1;
聽聽聽聽聽聽 std::cout << pc << std::endl;
聽聽聽聽聽聽 pc = a2;
聽聽聽聽聽聽 std::cout << pc << std::endl;
聽聽聽聽聽聽 const char* const pcc = "a const pointer to a const char array";
聽聽聽聽聽聽 std::cout << pcc << std::endl;
聽聽聽聽聽聽 // error C2166: l-value specifies const object
聽聽聽聽聽聽 // pcc = a1;聽 // error!
聽聽聽聽聽聽 std::cout << pcc << std::endl;
5. You can define a const variable in a class, but it must be static const, and have a definition in an implementation file.
// .h file
class try_const
{
public:
聽聽聽聽聽聽 static const int num;
};
// .cxx file
const int try_const::num = 250;
6. You can get all the efficiency of a macro plus all the predictable behavior and type safety of a regular function by using an inline function.
Template <class type>
Inline const type& max (const type& a, const type& b)
{
Return a > b ? a : b ;
}
7. Given the availability of consts and inlines, your need for the preprocessor is reduced, but it's not completely eliminated. The day is far from near when you can abandon #include, and #ifdef/#ifndef continue to play important roles in controlling compilation. It's not yet time to retire the preprocessor, but you should definitely plan to start giving it longer and more frequent vacations.
8. 聽scanf and printf are not type-safe and extensible.
9.聽 In particular, if you #include <iostream>, you get the elements of the iostream library ensconced within the namespace std (see Item 28), but if you #include <iostream.h>, you get those same elements at global scope. Getting them at global scope can lead to name conflicts, precisely the kinds of name conflicts the use of namespaces is designed to prevent.
10. The problem with malloc and free(and their variants) is simple : they don鈥檛 know about constructors and destructors.
11. free 鎿嶄綔涓嶄細璋冪敤鏋愭瀯鍑芥暟錛屽鏋滄寚閽堟墍鎸囧璞℃湰韜張鍒嗛厤浜嗗唴瀛橈紝鍒欎細閫犳垚鍐呭瓨涓㈠け銆?/span>
12. Memory management concerns in C++ fall into two general camps: getting it right and making it perform efficiently.
13. When you use new, two things happen. First, memory is allocated. Second, one or more constructors are called for that memory. When you use delete, two other things happen: one or more destructors are called for the memory, then the memory is deallocated.
14. The standard C++ library includes string and vector templates that reduce the need for built-in arrays to nearly zero.
15. Speaking of smart pointers, one way to avoid the need to delete pointer members is to replace those members with smart pointer objects like the standard C++ Library鈥檚 auto_ptr.
璁╂垜浠洖榪囧ご鍘葷湅鐪嬭繖鏍蜂竴涓熀鏈棶棰橈細涓轟粈涔堟湁蹇呰鍐欒嚜宸辯殑 operator new 鍜?/span> operator delete 錛熺瓟妗堥氬父鏄細涓轟簡鏁堢巼銆傜己鐪佺殑 operator new 鍜?/span> operator delete 鍏鋒湁闈炲父濂界殑閫氱敤鎬э紝瀹冪殑榪欑鐏墊椿鎬т篃浣垮緱鍦ㄦ煇浜涚壒瀹氱殑鍦哄悎涓嬶紝鍙互榪涗竴姝ユ敼鍠勫畠鐨勬ц兘銆傚挨鍏跺湪閭d簺闇瑕佸姩鎬佸垎閰嶅ぇ閲忕殑浣嗗緢灝忕殑瀵硅薄鐨勫簲鐢ㄧ▼搴忛噷錛屾儏鍐墊洿鏄姝ゃ?/span>
聽int* pi = new int(0);
聽
聽聽聽聽
if
聽(聽pi聽!=聽0聽)
聽聽聽聽聽聽聽聽delete聽pi;
聽
璇存槑錛氬鏋滄寚閽堟搷浣滄暟琚緗負0錛屽垯C++淇濊瘉delete琛ㄨ揪寮忎笉浼氳皟鐢ㄦ搷浣滅delete()銆傛墍浠ユ病鏈夊繀瑕佹祴璇曞叾鏄惁涓?span lang="EN-US">0銆?span lang="EN-US">
聽鍦?span lang="EN-US">delete琛ㄨ揪寮忎箣鍚庯紝pi琚О浣滅┖鎮寚閽堬紝鍗蟲寚鍚戞棤鏁堝唴瀛樼殑鎸囬拡銆傜┖鎮寚閽堟槸紼嬪簭閿欒鐨勬牴婧愶紝寤鴻瀵硅薄閲婃斁鍚庯紝灝嗚鎸囬拡璁劇疆涓?span lang="EN-US">0銆?span lang="EN-US">
聽
聽2錛?span lang="EN-US">auto_ptr
聽auto_ptr鏄?span lang="EN-US">C++鏍囧噯搴撴彁渚涚殑綾繪ā鏉匡紝瀹冨彲浠ュ府鍔╃▼搴忓憳鑷姩綆$悊鐢?span lang="EN-US">new琛ㄨ揪寮忓姩鎬佸垎閰嶇殑鍗曚釜瀵硅薄錛屼絾鏄紝瀹冩病鏈夊鏁扮粍綆$悊鎻愪緵綾諱技鏀寔銆傚畠鐨勫ご鏂囦歡涓猴細
聽
褰?span lang="EN-US">auto_ptr瀵硅薄鐨勭敓鍛芥湡緇撴潫鏃訛紝鍔ㄦ佸垎閰嶇殑瀵硅薄琚嚜鍔ㄩ噴鏀俱?span lang="EN-US">
聽auto_ptr綾繪ā鏉胯儗鍚庣殑涓昏鍔ㄦ満鏄敮鎸佷笌鏅氭寚閽堢被鍨嬬浉鍚岀殑璇硶錛屼絾鏄負auto_ptr瀵硅薄鎵鎸囧璞$殑閲婃斁鎻愪緵鑷姩綆$悊銆備緥錛?span lang="EN-US">
聽auto_ptr
綾繪ā鏉挎敮鎸佹墍鏈夋潈姒傚康錛屽綋涓涓?span lang="EN-US">auto_ptr瀵硅薄琚敤鍙︿竴涓?span lang="EN-US">auto_ptr瀵硅薄鍒濆鍖栬祴鍊兼椂錛屽乏杈硅璧嬪兼垨鍒濆鍖栫殑瀵硅薄灝辨嫢鏈変簡絀洪棽瀛樺偍鍖哄唴搴曞眰瀵硅薄鐨勬墍鏈夋潈錛岃屽彸杈圭殑auto_ptr瀵硅薄鍒?font color="#ff0000">鎾ゆ秷鎵鏈夎矗浠?/font>銆備緥錛?span lang="EN-US">
聽
鍒ゆ柇鏄惁鎸囧悜涓涓璞★紝渚嬶細
聽3錛夋暟緇勭殑鍔ㄦ佸垎閰嶄笌閲婃斁
聽寤鴻浣跨敤C++鏍囧噯搴?span lang="EN-US">string,閬垮厤浣跨敤C椋庢牸瀛楃涓叉暟緇勩?span lang="EN-US">
聽涓洪伩鍏嶅姩鎬佸垎閰嶆暟緇勭殑鍐呭瓨綆$悊甯︽潵鐨勯棶棰橈紝涓鑸緩璁嬌鐢ㄦ爣鍑嗗簱vector銆?span lang="EN-US">list鎴?span lang="EN-US">string瀹瑰櫒綾誨瀷銆?span lang="EN-US">
聽
聽4錛夊父閲忓璞$殑鍔ㄦ佸垎閰嶄笌閲婃斁
聽鍙互浣跨敤new琛ㄨ揪寮忓湪絀洪棽瀛樺偍鍖哄唴鍒涘緩涓涓?span lang="EN-US">const瀵硅薄錛屼緥錛?span lang="EN-US">
聽
鎴戜滑涓嶈兘鍦ㄧ┖闂插瓨鍌ㄥ尯鍒涘緩鍐呯疆綾誨瀷鍏冪礌鐨?span lang="EN-US">const鏁扮粍錛屽師鍥犳槸錛氭垜浠笉鑳藉垵濮嬪寲鐢?span lang="EN-US">new琛ㄨ揪寮忓垱寤虹殑鍐呯疆綾誨瀷鏁扮粍鐨勫厓绱犮備緥錛?span lang="EN-US">
聽5
錛夊畾浣?span lang="EN-US">new琛ㄨ揪寮?span lang="EN-US">
聽new琛ㄨ揪寮忕殑絎笁縐嶅艦寮忓厑璁哥▼搴忓憳瑕佹眰灝嗗璞″垱寤哄湪宸茬粡琚垎閰嶅ソ鐨勫唴瀛樹腑銆傜О涓猴細瀹氫綅new琛ㄨ揪寮忥紙placement new expression)銆傜▼搴忓憳鍦?span lang="EN-US">new琛ㄨ揪寮忎腑鎸囧畾寰呭垱寤哄璞℃墍鍦ㄧ殑鍐呭瓨鍦板潃銆傚涓嬫墍紺猴細
聽new 錛?span lang="EN-US">place_address) type-specifier
聽娉ㄦ剰錛?span lang="EN-US">place_address蹇呴』鏄釜鎸囬拡錛屽繀欏誨寘鍚ご鏂囦歡<new>銆傝繖欏硅鏂藉厑璁哥▼搴忓憳棰勫垎閰嶅ぇ閲忕殑鍐呭瓨錛屼緵浠ュ悗閫氳繃榪欑褰㈠紡鐨?span lang="EN-US">new琛ㄨ揪寮忓垱寤哄璞°備緥濡傦細
1. A declaration tells compilers about the name and type of an object, function, class, or template, but it omits certain details.
2. A definition, on the other hand, provides compilers with the details. For an object, the definition is where compilers allocate memory for the object. For a function or a function template, the definition provides the code body. For a class or a class template, the definition lists the members of the class or template.
3. When you define a class, you generally need a default constructor if you want to define arrays of objects.Incidentally, if you want to create an array of objects for which there is no default constructor, the usual ploy is to define an array of pointers instead. Then you can initialize each pointer separately by using new.
4. Probably the most important use of the copy constructor is to define what it means to pass and return objects by value.
5. From a purely operational point of view, the difference between initialization and assignment is that the former is performed by a constructor while the latter is performed by operator=. In other words, the two processes correspond to different function calls. The reason for the distinction is that the two kinds of functions must worry about different things. Constructors usually have to check their arguments for validity, whereas most assignment operators can take it for granted that their argument is legitimate (because it has already been constructed). On the other hand, the target of an assignment, unlike an object undergoing construction, may already have resources allocated to it. These resources typically must be released before the new resources can be assigned. Frequently, one of these resources is memory. Before an assignment operator can allocate memory for a new value, it must first deallocate the memory that was allocated for the old value.
6. These different casting forms serve different purposes:
const_cast is designed to cast away the constness of objects and pointers, a topic I examine in Item 21.
dynamic_cast is used to perform "safe downcasting," a subject we'll explore in Item 39.
reinterpret_cast is engineered for casts that yield implementation-dependent results, e.g., casting between function pointer types. (You're not likely to need reinterpret_cast very often. I don't use it at all in this book.)
static_cast is sort of the catch-all cast. It's what you use when none of the other casts is appropriate. It's the closest in meaning to the conventional C-style casts.