锘??xml version="1.0" encoding="utf-8" standalone="yes"?>久久综合亚洲色一区二区三区,国产午夜精品理论片久久影视,97久久精品无码一区二区http://www.shnenglu.com/keyws/category/2482.htmlC++ && keyWordSpottingzh-cnFri, 23 May 2008 19:43:44 GMTFri, 23 May 2008 19:43:44 GMT60銆恘ote銆慐ffective C++ (2) - Shifting from C to C++http://www.shnenglu.com/keyws/archive/2006/08/22/11580.htmlkeywskeywsTue, 22 Aug 2006 09:24:00 GMThttp://www.shnenglu.com/keyws/archive/2006/08/22/11580.htmlhttp://www.shnenglu.com/keyws/comments/11580.htmlhttp://www.shnenglu.com/keyws/archive/2006/08/22/11580.html#Feedback0http://www.shnenglu.com/keyws/comments/commentRss/11580.htmlhttp://www.shnenglu.com/keyws/services/trackbacks/11580.html Shifting from C to C++

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.

Item 1: Prefer const and inline to #define

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.

Item 2: Prefer <iostream> to <stdio.h>

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.

Item 3: Prefer new and delete to malloc and free

10. The problem with malloc and free(and their variants) is simple : they don鈥檛 know about constructors and destructors.

11. free 鎿嶄綔涓嶄細璋冪敤鏋愭瀯鍑芥暟錛屽鏋滄寚閽堟墍鎸囧璞℃湰韜張鍒嗛厤浜嗗唴瀛橈紝鍒欎細閫犳垚鍐呭瓨涓㈠け銆?/span>

Item 4: Prefer C++ style comments

Memory Management

12. Memory management concerns in C++ fall into two general camps: getting it right and making it perform efficiently.

Item 5: Use the same form in corresponding uses of new and delete

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.

Item 6: Use delete on pointer members in destructors

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.

Item 7: Be prepared for out-of-memory conditions

Item 8: Adhere to convention when writing operator new and operator delete

Item 9: Avoid hiding the 鈥渘ormal鈥?form of new

Item 10: Write operator delete if you write operator new

璁╂垜浠洖榪囧ご鍘葷湅鐪嬭繖鏍蜂竴涓熀鏈棶棰橈細涓轟粈涔堟湁蹇呰鍐欒嚜宸辯殑 operator new 鍜?/span> operator delete 錛熺瓟妗堥氬父鏄細涓轟簡鏁堢巼銆傜己鐪佺殑 operator new 鍜?/span> operator delete 鍏鋒湁闈炲父濂界殑閫氱敤鎬э紝瀹冪殑榪欑鐏墊椿鎬т篃浣垮緱鍦ㄦ煇浜涚壒瀹氱殑鍦哄悎涓嬶紝鍙互榪涗竴姝ユ敼鍠勫畠鐨勬ц兘銆傚挨鍏跺湪閭d簺闇瑕佸姩鎬佸垎閰嶅ぇ閲忕殑浣嗗緢灝忕殑瀵硅薄鐨勫簲鐢ㄧ▼搴忛噷錛屾儏鍐墊洿鏄姝ゃ?/span>



keyws 2006-08-22 17:24 鍙戣〃璇勮
]]>
銆恘ote銆戙奀++ Primer 錛?8.4鑺傗滃姩鎬佸垎閰嶇殑瀵硅薄鈥濄嬪涔犵瑪璁?http://www.shnenglu.com/keyws/archive/2006/08/22/11573.htmlkeywskeywsTue, 22 Aug 2006 06:54:00 GMThttp://www.shnenglu.com/keyws/archive/2006/08/22/11573.htmlhttp://www.shnenglu.com/keyws/comments/11573.htmlhttp://www.shnenglu.com/keyws/archive/2006/08/22/11573.html#Feedback2http://www.shnenglu.com/keyws/comments/commentRss/11573.htmlhttp://www.shnenglu.com/keyws/services/trackbacks/11573.html
聽聽聽鍔ㄦ佸垎閰嶇殑瀵硅薄錛氱▼搴忓憳瀹屽叏鎺у埗鍒嗛厤涓庨噴鏀撅紝鍒嗛厤鍦ㄧ▼搴忕殑絀洪棽瀛樺偍鍖猴紙free store)鐨勫彲鐢ㄥ唴瀛樻睜涓?span lang="EN-US">

聽1
錛夊崟涓璞$殑鍔ㄦ佸垎閰嶄笌閲婃斁錛?span lang="EN-US">
聽new琛ㄨ揪寮忔病鏈夎繑鍥炲疄闄呭垎閰嶇殑瀵硅薄錛岃屾槸榪斿洖鎸囧悜璇ュ璞$殑鎸囬拡銆傚璇ュ璞$殑鍏ㄩ儴鎿嶄綔閮借閫氳繃榪欎釜鎸囬拡闂存帴瀹屾垚銆?span lang="EN-US">
聽闅忔満鍒嗛厤鐨勫唴瀛樺叿鏈夐殢鏈虹殑浣嶆ā寮忥紝寤鴻鍒濆鍖栵紝渚嬪錛?span lang="EN-US">

聽int* pi = new int(0);


絀洪棽瀛樺偍鍖烘槸鏈夐檺鐨勮祫婧愶紝鑻ヨ鑰楀敖錛?span lang="EN-US">new琛ㄨ揪寮忎細澶辮觸錛屾姏鍑?span lang="EN-US">bad_alloc寮傚父銆?span lang="EN-US">
聽榪欐牱鍋氭病鏈夊繀瑕?span lang="EN-US">:

聽聽聽聽 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琛ㄨ揪寮忓姩鎬佸垎閰嶇殑鍗曚釜瀵硅薄錛屼絾鏄紝瀹冩病鏈夊鏁扮粍綆$悊鎻愪緵綾諱技鏀寔銆傚畠鐨勫ご鏂囦歡涓猴細

聽聽聽聽#include聽 < memory >

褰?span lang="EN-US">auto_ptr瀵硅薄鐨勭敓鍛芥湡緇撴潫鏃訛紝鍔ㄦ佸垎閰嶇殑瀵硅薄琚嚜鍔ㄩ噴鏀俱?span lang="EN-US">
聽auto_ptr綾繪ā鏉胯儗鍚庣殑涓昏鍔ㄦ満鏄敮鎸佷笌鏅氭寚閽堢被鍨嬬浉鍚岀殑璇硶錛屼絾鏄負auto_ptr瀵硅薄鎵鎸囧璞$殑閲婃斁鎻愪緵鑷姩綆$悊銆備緥錛?span lang="EN-US">

聽聽聽聽 //聽 絎竴縐嶅垵濮嬪寲褰㈠紡
聽聽聽聽std::auto_ptr< int >聽pi(聽 new int (1024)聽);聽聽聽聽 //聽

聽auto_ptr 綾繪ā鏉挎敮鎸佹墍鏈夋潈姒傚康錛屽綋涓涓?span lang="EN-US">auto_ptr瀵硅薄琚敤鍙︿竴涓?span lang="EN-US">auto_ptr瀵硅薄鍒濆鍖栬祴鍊兼椂錛屽乏杈硅璧嬪兼垨鍒濆鍖栫殑瀵硅薄灝辨嫢鏈変簡絀洪棽瀛樺偍鍖哄唴搴曞眰瀵硅薄鐨勬墍鏈夋潈錛岃屽彸杈圭殑auto_ptr瀵硅薄鍒?font color="#ff0000">鎾ゆ秷鎵鏈夎矗浠?/font>銆備緥錛?span lang="EN-US">

聽聽聽聽std::auto_ptr<std:: string >聽pstr_auto(聽 new 聽std:: string (聽"Brontonsaurus"聽)聽);
聽聽聽聽
//聽
絎簩縐嶅垵濮嬪寲褰㈠紡
聽聽聽聽std::auto_ptr<std:: string >聽pstr_auto2(聽pstr_auto聽);

鍒ゆ柇鏄惁鎸囧悜涓涓璞★紝渚嬶細

聽聽聽聽 // 聽絎笁縐嶅垵濮嬪寲褰㈠紡
聽聽聽聽auto_ptr < int > 聽p_auto_int;聽聽聽聽
聽聽聽聽
if 聽(聽p_auto_int. get ()聽 == 0 聽)
聽聽聽聽聽聽聽聽
聽聽聽聽
else
聽聽聽聽聽聽聽聽
// 聽閲嶇疆搴曞眰鎸囬拡錛屽繀欏諱嬌鐢ㄦ鍑芥暟聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽p_auto_int.reset(聽 new int (聽 1024 聽)聽);


聽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">

聽聽聽聽 //聽 姝ゆ椂蹇呴』鍒濆鍖栵紝鍚﹀垯緙栬瘧閿欒
聽聽聽聽 const int *聽pci聽=聽 new const int (1024);聽聽聽聽

鎴戜滑涓嶈兘鍦ㄧ┖闂插瓨鍌ㄥ尯鍒涘緩鍐呯疆綾誨瀷鍏冪礌鐨?span lang="EN-US">const鏁扮粍錛屽師鍥犳槸錛氭垜浠笉鑳藉垵濮嬪寲鐢?span lang="EN-US">new琛ㄨ揪寮忓垱寤虹殑鍐呯疆綾誨瀷鏁扮粍鐨勫厓绱犮備緥錛?span lang="EN-US">

聽聽聽聽 const int *聽pci聽=聽 new const int [100];聽 //聽 緙栬瘧閿欒


聽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琛ㄨ揪寮忓垱寤哄璞°備緥濡傦細

聽聽聽聽#include聽 < iostream >
聽聽聽聽#include聽
< new > 聽聽聽聽 // 聽蹇呴』鍖呭惈榪欎釜澶存枃浠?/span>
聽聽聽聽
聽聽聽聽
const int 聽chunk聽 = 16 ;
聽聽聽聽
class 聽Foo
聽聽聽聽
{
聽聽聽聽聽聽聽聽
聽聽聽聽}
;
聽聽聽聽
聽聽聽聽
char * 聽buf聽 = new char [聽 sizeof (Foo)聽 * 聽chunk聽];
聽聽聽聽
聽聽聽聽
int 聽main( int 聽argc,聽 char * 聽argv[])
聽聽聽聽
{
聽聽聽聽聽聽聽聽
// 聽鍙湁榪欑褰㈠紡鐨勫垱寤猴紝娌℃湁閰嶅褰㈠紡鐨刣elete聽
聽聽聽聽聽聽聽聽Foo * 聽pb聽 = new 聽(buf)聽Foo;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽delete[]聽buff;
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
return 0 ;
聽聽聽聽}



keyws 2006-08-22 14:54 鍙戣〃璇勮
]]>
銆恘ote銆慐ffective C++ (1) - Introduction http://www.shnenglu.com/keyws/archive/2006/08/20/11495.htmlkeywskeywsSun, 20 Aug 2006 08:10:00 GMThttp://www.shnenglu.com/keyws/archive/2006/08/20/11495.htmlhttp://www.shnenglu.com/keyws/comments/11495.htmlhttp://www.shnenglu.com/keyws/archive/2006/08/20/11495.html#Feedback0http://www.shnenglu.com/keyws/comments/commentRss/11495.htmlhttp://www.shnenglu.com/keyws/services/trackbacks/11495.html Introduction

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.

// 聽a聽possible聽String聽constructor
String::String( const char * value)
{
聽聽聽聽
if 聽(value)
聽聽聽聽
{聽
聽聽聽聽聽聽聽聽
// 聽if聽value聽ptr聽isn't聽null
聽聽聽聽聽聽聽聽data聽 = new char [strlen(value)聽 + 1 ];
聽聽聽聽聽聽聽聽strcpy(data,value);
聽聽聽聽}
聽聽聽聽
聽聽聽聽
else
聽聽聽聽
{聽
聽聽聽聽聽聽聽聽
// 聽handle聽null聽value聽ptr3
聽聽聽聽聽聽聽聽data聽 = new char [ 1 ];
聽聽聽聽聽聽聽聽
* data聽 = ' \0 ' ;聽 // 聽add聽trailing
聽聽聽聽聽聽聽聽 null char
聽聽聽聽}

}


// 聽a聽possible聽String聽assignment聽operator

String
& 聽String:: operator = ( const 聽String & 聽rhs)
{
聽聽聽聽
if 聽( this == & rhs)
聽聽聽聽聽聽聽聽
return * this ;聽 // 聽see聽Item聽17

聽聽聽聽delete聽[]聽data;聽
// 聽delete聽old聽memory
聽聽聽聽
聽聽聽聽data聽
= // 聽allocate聽new聽memory
聽聽聽聽聽聽聽聽 new char [strlen(rhs.data)聽 + 1 ];

聽聽聽聽strcpy(data,聽rhs.data);
聽聽聽聽
聽聽聽聽
return * this ;聽 // 聽see聽Item聽15
}


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.



keyws 2006-08-20 16:10 鍙戣〃璇勮
]]>
亚洲色婷婷综合久久| 国产午夜精品久久久久免费视| 精品无码久久久久久久久久| 久久久久九国产精品| 人妻少妇精品久久| 久久影院综合精品| 国产一区二区精品久久岳| 亚洲国产成人久久精品99 | 午夜精品久久久久久影视riav| 波多野结衣AV无码久久一区| 成人久久综合网| 久久综合久久美利坚合众国| 国内精品久久人妻互换| 久久久黄片| 久久精品国产亚洲网站| 综合久久精品色| 国产精品熟女福利久久AV| 亚洲中文字幕无码久久综合网| 91精品国产高清久久久久久国产嫩草| 少妇熟女久久综合网色欲| 99久久99久久精品国产| 综合人妻久久一区二区精品| 天天综合久久一二三区| 久久精品国产99国产精品澳门| 伊人久久久AV老熟妇色| 久久午夜无码鲁丝片午夜精品| 久久精品无码专区免费青青| 2021国内久久精品| 天天做夜夜做久久做狠狠| 99久久精品国产一区二区| 久久91精品久久91综合| 国内精品伊人久久久久av一坑 | 久久婷婷五月综合成人D啪| 国産精品久久久久久久| 久久精品国产影库免费看| 久久亚洲精品人成综合网| 欧美噜噜久久久XXX| 久久久久久久精品成人热色戒| 欧美与黑人午夜性猛交久久久| 久久高潮一级毛片免费| 99久久人人爽亚洲精品美女|