c++的設計者bjarne stroustrup下了很大的功夫想使使用者自訂類型儘可能地和固定類型的工作方式相似。這就是為什麼你可以重載運算子,寫類型轉換函式(見條款m5),控制賦值和拷貝建構函式,等等。他做了這麼多努力,那你最少也該繼續做下去。讓我們看看賦值。用固定類型的情況下,賦值操作可以象下面這樣鏈起來:int w, x, y, z;w = x = y = z = 0;所以,你也應該可以將使用者自訂類型的賦值操作鏈結起來:string w, x, y, z;// string是由標準c++
Item 45. BOOLDifficulty: 7Do we really need a builtin bool type? Why not just emulate it in the existing language? This Item reveals the answer.Besides wchar_t (which was a typedef in C), bool is the only builtin type to be added to C++ since the
Item 46. Forwarding FunctionsDifficulty: 3What's the best way to write a forwarding function? The basic answer is easy, but we'll also learn about a subtle change to the language made shortly before the standard was finalized.Forwarding functions
I l@ve RuBoard Item 44. CastsDifficulty: 6How well do you know C++'s casts? Using them well can greatly improve the reliability of your code.The new-style casts in standard C++ offer more power and safety than the old-style (C-style) casts. How
To ensure that the addresses of two different objects will be different. For the same reason, "new" always returns pointers to distinct objects. Consider:class Empty { };void f(){Empty a, b;if (&a == &b) cout << "impossible: report
Yes, but why do you want to? There are two common answers: for efficiency: to avoid my function calls being virtual for safety: to ensure that my class is not used as a base class (for example, to be sure that I can copy objects without fear of
This first problem highlights the importance of understanding what you write. Here we have four simple lines of code, no two of which mean the same thing, even though the syntax varies only slightly.What is the difference, if any, between the