锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
1.const int r=100; //鏍囧噯const鍙橀噺澹版槑鍔犲垵濮嬪寲錛屽洜涓洪粯璁ゅ唴閮ㄨ繛鎺ユ墍浠ュ繀欏昏鍒濆鍖栵紝鍏朵綔鐢ㄥ煙涓烘鏂囦歡錛岀紪璇戝櫒緇忚繃綾誨瀷媯鏌ュ悗鐩存帴鐢?00鍦ㄧ紪璇戞椂鏇挎崲銆?nbsp;
2.extend const int r=100; //灝哻onst鏀逛負澶栭儴榪炴帴錛屼綔鐢ㄤ簬鎵╁ぇ鑷沖叏灞錛岀紪璇戞椂浼氬垎閰嶅唴瀛橈紝騫朵笖鍙互涓嶈繘琛屽垵濮嬪寲錛屼粎浠呬綔涓哄0鏄庯紝緙栬瘧鍣ㄨ涓哄湪紼嬪簭鍏朵粬鍦版柟榪涜浜嗗畾涔夈?nbsp;
3.const int r[ ]={1,2,3,4};
struct S {int a,b;};
const S s[ ]={(1,2),(3.4)}; //浠ヤ笂涓ょ閮芥槸甯擱噺闆嗗悎錛岀紪璇戝櫒浼氫負鍏跺垎閰嶅唴瀛橈紝鎵浠ヤ笉鑳藉湪緙栬瘧鏈熼棿浣跨敤鍏朵腑鐨勫鹼紝渚嬪錛歩nt temp[r[2]];榪欐牱鐨勭紪璇戝櫒浼氭姤鍛婁笉鑳芥壘鍒板父閲忚〃杈懼紡
浜屻佸浜庢寚閽?nbsp;
1.const int *r=&x; //澹版槑r涓轟竴涓寚鍚戝父閲忕殑x鐨勬寚閽堬紝r鎸囧悜鐨勫璞′笉鑳借淇敼錛屼絾浠栧彲浠ユ寚鍚戜換浣曞湴鍧鐨勫父閲忋?nbsp;
2.int const *r=&x; //涓庣敤娉?瀹屽叏絳変環錛屾病鏈変換浣曞尯鍒?nbsp;
3.int * const r=&x; //澹版槑r涓轟竴涓父閲忔寚閽堬紝浠栨寚鍚憍錛宺榪欎釜鎸囬拡鐨勬寚鍚戜笉鑳借淇敼錛屼絾浠栨寚鍚戠殑鍦板潃鐨勫唴瀹瑰彲浠ヤ慨鏀廣?nbsp;
4.const int * const r=&x; //緇煎悎1銆?鐢ㄦ硶錛宺鏄竴涓寚鍚戝父閲忕殑甯擱噺鍨嬫寚閽堛?nbsp;
涓夈佸浜庣被鍨嬫鏌?nbsp;
鍙互鎶婁竴涓潪const瀵硅薄璧嬬粰涓涓寚鍚慶onst鐨勬寚閽堬紝鍥犱負鏈夋椂鍊欐垜浠笉鎯充粠榪欎釜鎸囬拡鏉ヤ慨鏀瑰叾瀵硅薄鐨勫鹼紱浣嗘槸涓嶅彲浠ユ妸涓涓猚onst瀵硅薄璧嬪肩粰涓涓潪const鎸囬拡錛屽洜涓鴻繖鏍峰彲鑳戒細閫氳繃榪欎釜鎸囬拡鏀瑰彉鎸囧悜瀵硅薄鐨勫鹼紝浣嗕篃瀛樺湪浣胯繖縐嶆搷浣滈氳繃鐨勫悎娉曞寲鍐欐硶錛屼嬌鐢ㄧ被鍨嬪己鍒惰漿鎹㈠彲浠ラ氳繃鎸囬拡鏀瑰彉const瀵硅薄錛?nbsp;
const int r=100;
int * ptr = const_cast<int *>(&r); //C++鏍囧噯錛孋璇█浣跨敤錛歩nt * ptr =(int*)&r;
鍥涖佸浜庡瓧絎︽暟緇?nbsp;
濡俢har * name = “china”; 榪欐牱鐨勮鍙ワ紝鍦ㄧ紪璇戞椂鏄兘澶熼氳繃鐨勶紝浣嗘槸”china”鏄父閲忓瓧絎︽暟緇勶紝浠諱綍鎯充慨鏀逛粬鐨勬搷浣滀篃鑳介氳繃緙栬瘧浣嗕細寮曡搗榪愯鏃墮敊璇紝濡傛灉鎴戜滑鎯充慨鏀瑰瓧絎︽暟緇勭殑璇濆氨瑕佷嬌鐢╟har name[ ] = “china”; 榪欑褰㈠紡銆?nbsp;
浜斻佸浜庡嚱鏁?nbsp;
1.void Fuction1 ( const int r ); //姝ゅ涓哄弬鏁頒紶閫抍onst鍊鹼紝鎰忎箟鏄彉閲忓垵鍊間笉鑳借鍑芥暟鏀瑰彉
2.const int Fuction1 (int); //姝ゅ榪斿洖const鍊鹼紝鎰忔濇寚榪斿洖鐨勫師鍑芥暟閲岀殑鍙橀噺鐨勫垵鍊間笉鑳借淇敼錛屼絾鏄嚱鏁版寜鍊艱繑鍥炵殑榪欎釜鍙橀噺琚埗鎴愬壇鏈紝鑳戒笉鑳借淇敼灝辨病鏈変簡鎰忎箟錛屽畠鍙互琚祴緇欎換浣曠殑const鎴栭潪const綾誨瀷鍙橀噺錛屽畬鍏ㄤ笉闇瑕佸姞涓婅繖涓猚onst鍏抽敭瀛椼備絾榪欏彧瀵逛簬鍐呴儴綾誨瀷鑰岃█錛堝洜涓哄唴閮ㄧ被鍨嬭繑鍥炵殑鑲畾鏄竴涓鹼紝鑰屼笉浼氳繑鍥炰竴涓彉閲忥紝涓嶄細浣滀負宸﹀間嬌鐢級錛屽浜庣敤鎴瘋嚜瀹氫箟綾誨瀷錛岃繑鍥炲兼槸甯擱噺鏄潪甯擱噸瑕佺殑錛岃涓嬮潰鏉℃3銆?nbsp;
3.Class CX; //鍐呴儴鏈夋瀯閫犲嚱鏁幫紝澹版槑濡侰X(int r =0)
CX Fuction1 () { return CX(); }
const CX Fuction2 () { return CX(); }
濡傛湁涓婇潰鐨勮嚜瀹氫箟綾籆X錛屽拰鍑芥暟Fuction1()鍜孎uction2(),鎴戜滑榪涜濡備笅鎿嶄綔鏃訛細
Fuction1() = CX(1); //娌℃湁闂錛屽彲浠ヤ綔涓哄乏鍊艱皟鐢?nbsp;
Fuction2() = CX(1); //緙栬瘧閿欒錛宑onst榪斿洖鍊肩姝綔涓哄乏鍊艱皟鐢ㄣ傚洜涓哄乏鍊兼妸榪斿洖鍊間綔涓哄彉閲忎細淇敼鍏惰繑鍥炲鹼紝const澹版槑紱佹榪欑淇敼銆?nbsp;
4.鍑芥暟涓寚閽堢殑const浼犻掑拰榪斿洖錛?nbsp;
int F1 (const char * pstr); //浣滀負浼犻掔殑鏃跺欎嬌鐢╟onst淇グ鍙互淇濊瘉涓嶄細閫氳繃榪欎釜鎸囬拡鏉ヤ慨鏀逛紶閫掑弬鏁扮殑鍒濆鹼紝榪欓噷鍦ㄥ嚱鏁板唴閮ㄤ換浣曚慨鏀?pstr鐨勪紒鍥鵑兘浼氬紩璧風紪璇戦敊璇?nbsp;
const char * F2 (); //鎰忎箟鏄嚱鏁拌繑鍥炵殑鎸囬拡鎸囧悜鐨勫璞℃槸涓涓猚onst瀵硅薄錛屽畠蹇呴』璧嬬粰涓涓悓鏍鋒槸鎸囧悜const瀵硅薄鐨勬寚閽堛?nbsp;
const char * const F3(); //姣斾笂闈㈠浜嗕竴涓猚onst錛岃繖涓猚onst鐨勬剰涔夊彧鏄湪浠栬鐢ㄤ綔宸﹀兼椂鏈夋晥錛屽畠琛ㄦ槑浜嗚繖涓寚閽堥櫎浜嗘寚鍚慶onst瀵硅薄澶栵紝瀹冩湰韜篃涓嶈兘琚慨鏀癸紝鎵浠ュ氨涓嶈兘褰撲綔宸﹀兼潵澶勭悊銆?nbsp;
5.鍑芥暟涓紩鐢ㄧ殑const浼犻掞細
void F1 ( const X& px); //榪欐牱鐨勪竴涓猚onst寮曠敤浼犻掑拰鏈鏅氱殑鍑芥暟鎸夊間紶閫掔殑鏁堟灉鏄竴妯′竴鏍風殑錛屼粬紱佹瀵瑰紩鐢ㄧ殑瀵硅薄鐨勪竴鍒囦慨鏀癸紝鍞竴涓嶅悓鐨勬槸鎸夊間紶閫掍細鍏堝緩绔嬩竴涓被瀵硅薄鐨勫壇鏈紝鐒跺悗浼犻掕繃鍘伙紝鑰屽畠鐩存帴浼犻掑湴鍧錛屾墍浠ヨ繖縐嶄紶閫掓瘮鎸夊間紶閫掓洿鏈夋晥銆?nbsp;
**鍙﹀鍙湁寮曠敤鐨刢onst浼犻掑彲浠ヤ紶閫掍竴涓復鏃跺璞★紝鍥犱負涓存椂瀵硅薄閮芥槸const灞炴э紝涓旀槸涓嶅彲瑙佺殑錛屼粬鐭椂闂村瓨鍦ㄤ竴涓眬閮ㄥ煙涓紝鎵浠ヤ笉鑳戒嬌鐢ㄦ寚閽堬紝鍙湁寮曠敤鐨刢onst浼犻掕兘澶熸崟鎹夊埌榪欎釜瀹朵紮銆?nbsp;
鍏佸浜庣被
1.棣栧厛錛屽浜巆onst鐨勬垚鍛樺彉閲忥紝鍙兘鍦ㄦ瀯閫犲嚱鏁伴噷浣跨敤鍒濆鍖栨垚鍛樺垪琛ㄦ潵鍒濆鍖栵紝璇曞浘鍦ㄦ瀯閫犲嚱鏁頒綋鍐呰繘琛屽垵濮嬪寲const鎴愬憳鍙橀噺浼氬紩璧風紪璇戦敊璇傚垵濮嬪寲鎴愬憳鍒楄〃褰㈠錛?nbsp;
2.X:: X ( int ir ): r(ir) {} //鍋囪r鏄被X鐨刢onst鎴愬憳鍙橀噺
2.const鎴愬憳鍑芥暟銆傛彁鍒拌繖涓蹇甸鍏堣璋堝埌const瀵硅薄錛屾璞″唴緗被鍨嬭兘澶熷畾涔塩onst瀵硅薄涓鏍鳳紙const int r=10;錛夛紝鐢ㄦ埛鑷畾涔夌被鍨嬩篃鍙互瀹氫箟const瀵硅薄(const X px(10);)錛岀紪璇戝櫒瑕佷繚璇佽繖涓璞″湪鍏剁敓鍛藉懆鏈熷唴涓嶈兘澶熻鏀瑰彉銆傚鏋滀綘瀹氫箟浜嗚繖鏍風殑涓涓猚onst瀵硅薄錛岄偅涔堝浜庤繖涓璞$殑涓鍒囬潪const鎴愬憳鍑芥暟鐨勮皟鐢紝緙栬瘧鍣ㄤ負浜嗕繚璇佸璞$殑const鐗規э紝閮戒細紱佹騫跺湪緙栬瘧鏈熼棿鎶ラ敊銆傛墍浠ュ鏋滀綘鎯寵浣犵殑鎴愬憳鍑芥暟鑳藉鍦╟onst瀵硅薄涓婅繘琛屾搷浣滅殑璇濓紝灝辮鎶婅繖涓嚱鏁板0鏄庝負const鎴愬憳鍑芥暟銆傚亣濡俧( )鏄被涓殑鎴愬憳鍑芥暟鐨勮瘽錛屽畠鐨勫0鏄庡艦濡傦細
int f( ) const; //const鏀懼湪鍑芥暟鐨勬渶鍚庯紝緙栬瘧鍣ㄤ細瀵硅繖涓嚱鏁拌繘琛屾鏌ワ紝鍦ㄨ繖涓嚱鏁頒腑鐨勪換浣曡瘯鍥炬敼鍙樻垚鍛樺彉閲忓拰璋冪敤闈瀋onst鎴愬憳鍑芥暟鐨勬搷浣滈兘琚涓洪潪娉?nbsp;
娉ㄦ剰錛氱被鐨勬瀯閫犲拰鏋愭瀯鍑芥暟閮戒笉鑳芥槸const鍑芥暟銆?nbsp;
3.寤虹珛浜嗕竴涓猚onst鎴愬憳鍑芥暟錛屼絾浠嶇劧鎯崇敤榪欎釜鍑芥暟鏀瑰彉瀵硅薄鍐呴儴鐨勬暟鎹傝繖鏍風殑涓涓姹備篃浼氱粡甯擱亣鍒幫紝灝ゅ叾鏄湪涓涓嫑鍒葷殑闈㈣瘯鑰冨畼閭i噷銆傞鍏堟垜浠寮勬竻妤氳冨畼鐨勮姹傦紝鍥犱負鏈変袱縐嶆柟娉曞彲浠ュ疄鐜幫紝濡傛灉榪欎綅鑰冨畼瑕佹眰涓嶆敼鍙樺師鏉ョ被鐨勪換浣曚笢瑗匡紝鍙浣犱粠褰撳墠榪欎釜const鎴愬憳鍑芥暟鍏ユ墜錛岄偅涔堜綘鍙湁浣跨敤鍓嶉潰鎻愬埌鐨勭被鍨嬪己鍒惰漿鎹㈡柟娉曘傚疄渚嬪涓嬶細
//鍋囧鏈変竴涓彨鍋歑鐨勭被錛屽畠鏈変竴涓猧nt鎴愬憳鍙橀噺r錛屾垜浠渶瑕侀氳繃涓涓猚onst鎴愬憳鍑芥暟f( )鏉ュ榪欎釜r榪涜++r鎿嶄綔錛屼唬鐮佸涓?nbsp;
void X::f( ) const
{ (const_cast(this)) -> ++r; } //閫氳繃this鎸囬拡榪涜綾誨瀷寮哄埗杞崲瀹炵幇
鍙﹀涓縐嶆柟娉曞氨鏄嬌鐢ㄥ叧閿瓧錛歮utable銆傚鏋滀綘鐨勬垚鍛樺彉閲忓湪瀹氫箟鏃舵槸榪欎釜鏍峰瓙鐨勶細
mutable int r ;
閭d箞瀹冨氨鍛婅瘔緙栬瘧鍣ㄨ繖涓垚鍛樺彉閲忓彲浠ラ氳繃const鎴愬憳鍑芥暟鏀瑰彉銆傜紪璇戝櫒灝變笉浼氬啀鐞嗕細瀵逛粬鐨勬鏌ヤ簡銆?nbsp;
]]>
]]>
{
int i;
int a[10];
for(i=0; i<=10; i++)
{
a[i]=0;
printf("%d ",a[i]);
}
return 0;
}
]]>
http://www.hansencode.cn/category/tinyxml/
]]>
浜? 闂鐨勬彁鍑?br>C++ 涓嶆敮鎸佸弻鍒嗘淳錛屽彧鏀寔鍗曞垎媧俱備篃灝辨槸璇村弬鏁扮殑綾誨瀷灝嗗湪緙栬瘧闃舵鍐沖畾銆?铏界劧閬囦笂鍚屽悕鍑芥暟鐨勯夊彇鏃訛紝娌℃湁浣撶幇澶氭侊紝浣嗚繘鍏ュ嚱鏁板悗錛屾寚閽堝弬鏁版垨寮曠敤鍙傛暟浠嶇劧琛ㄧ幇浜嗗叾澶氭?
涓嬮潰鐨勪唬鐮佹紨紺轟簡璇ラ棶棰橈細
using namespace std;
class D;
//B
class B
{
public:
void virtual output(B * b){cout << "B:B" << endl;}
void virtual output(D * d){cout << "B:D" << endl;}
};
class D : public B
{
public:
void output(B * b){cout << "D:B" << endl;}
void output(D * d){cout << "D:D" << endl;}
};
int main()
{
B * p1 = new D;
B * p2 = new D;
p1->output(p2);
return 0;
}D:B
涓? 瑙e喅鏂規硶
Visitor璁捐妯″紡
]]>template<class T> class Widget; // uses "class"
template<typename T> class Widget; // uses "typename"
銆銆鐒惰岋紝C++ 騫朵笉鎬繪槸鎶?class 鍜?typename 瑙嗕負絳夊悓鐨勪笢瑗褲傛湁鏃朵綘蹇呴』浣跨敤 typename銆備負浜嗙悊瑙h繖涓鐐癸紝鎴戜滑涓嶅緱涓嶈璁轟綘浼氬湪涓涓?template錛堟ā鏉匡級涓秹鍙婂埌鐨勪袱縐嶅悕瀛椼?br>
銆銆鍋囪鎴戜滑鏈変竴涓嚱鏁扮殑妯℃澘錛屽畠鑳藉彇寰椾竴涓?STL-compatible container錛圫TL 鍏煎瀹瑰櫒錛変腑鎸佹湁鐨勮兘璧嬪肩粰 ints 鐨勫璞°傝繘涓姝ュ亣璁捐繖涓嚱鏁板彧鏄畝鍗曞湴鎵撳嵃瀹冪殑絎簩涓厓绱犵殑鍊箋傚畠鏄竴涓敤緋婃秱鐨勬柟娉曞疄鐜扮殑緋婃秱鐨勫嚱鏁幫紝鑰屼笖灝卞儚鎴戜笅闈㈠啓鐨勶紝瀹冪敋鑷充笉鑳界紪璇戯紝浣嗘槸璇峰皢榪欎簺浜嬪厛鏀懼湪涓杈光斺旀湁涓縐嶆柟娉曡兘鍙戠幇鎴戠殑鎰氳牏錛?
template<typename C> // print 2nd element in
void print2nd(const C& container) // container;
{
銆// this is not valid C++!
銆if (container.size() >= 2)
{
銆銆C::const_iterator iter(container.begin()); // get iterator to 1st element
銆銆++iter; // move iter to 2nd element
銆銆int value = *iter; // copy that element to an int
銆銆std::cout << value; // print the int
銆}
}
銆銆print2nd 涓殑鍙︿竴涓?local variable錛堝眬閮ㄥ彉閲忥級value 鍏鋒湁 int 綾誨瀷銆俰nt 鏄竴涓笉渚濊禆浜庝換浣?template parameter錛堟ā鏉垮弬鏁幫級鐨勫悕瀛椼傝繖鏍風殑鍚嶅瓧浠?non-dependent names錛堥潪渚濊禆鍚嶅瓧錛夐椈鍚嶃傦紙鎴戞兂涓嶉氫負浠涔堜粬浠笉縐板畠涓?independent names錛堟棤渚濊禆鍚嶅瓧錛夈傚鏋滐紝鍍忔垜涓鏍鳳紝浣犲彂鐜版湳璇?"non-dependent" 鏄竴涓護浜哄帉鎭剁殑涓滆タ錛屼綘灝卞拰鎴戜駭鐢熶簡鍏遍福錛屼絾鏄?"non-dependent" 灝辨槸榪欑被鍚嶅瓧鐨勬湳璇紝鎵浠ワ紝鍍忔垜涓鏍鳳紝杞漿鐪肩潧鏀懼純浣犵殑鑷垜涓誨紶銆傦級
銆銆nested dependent name錛堝祵濂椾緷璧栧悕瀛楋級浼氬鑷磋В鏋愬洶闅俱備緥濡傦紝鍋囪鎴戜滑鏇村姞鎰氳牏鍦頒互榪欑鏂規硶寮濮?print2nd錛?br>
template<typename C>
void print2nd(const C& container)
{
銆C::const_iterator * x;
銆//
}
銆銆鐩村埌 C 鎴愪負宸茬煡涔嬪墠錛屾病鏈変換浣曞姙娉曠煡閬?C::const_iterator 鍒板簳鏄笉鏄竴涓?type錛堢被鍨嬶級錛岃屽綋 template錛堟ā鏉匡級print2nd 琚В鏋愮殑鏃跺欙紝C 榪樹笉鏄凡鐭ョ殑銆侰++ 鏈変竴鏉¤鍒欒В鍐寵繖涓涔夛細濡傛灉瑙f瀽鍣ㄥ湪涓涓?template錛堟ā鏉匡級涓亣鍒頒竴涓?nested dependent name錛堝祵濂椾緷璧栧悕瀛楋級錛屽畠鍋囧畾閭d釜鍚嶅瓧涓嶆槸涓涓?type錛堢被鍨嬶級錛岄櫎闈炰綘鐢ㄥ叾瀹冩柟寮忓憡璇夊畠銆傜己鐪佹儏鍐典笅錛宯ested dependent name錛堝祵濂椾緷璧栧悕瀛楋級涓嶆槸 types錛堢被鍨嬶級銆傦紙瀵逛簬榪欐潯瑙勫垯鏈変竴涓緥澶栵紝鎴戝緟浼氬効鍛婅瘔浣犮傦級
銆銆璁頒綇榪欎釜錛屽啀鐪嬬湅 print2nd 鐨勫紑澶達細
void print2nd(const C& container)
{
銆if (container.size() >= 2) {
銆銆C::const_iterator iter(container.begin()); // this name is assumed to
銆銆 // not be a type
void print2nd(const C& container)
{
if (container.size() >= 2) {
typename C::const_iterator iter(container.begin());
}
}
銆銆typename 搴旇浠呬粎琚敤浜庢爣璇?nested dependent type name錛堝祵濂椾緷璧栫被鍨嬪悕錛夛紱鍏跺畠鍚嶅瓧涓嶅簲璇ョ敤瀹冦備緥濡傦紝榪欐槸涓涓彇寰椾竴涓?container錛堝鍣級鍜岃繖涓?container錛堝鍣級涓殑涓涓?iterator錛堣凱浠e櫒錛夌殑 function template錛堝嚱鏁版ā鏉匡級錛?br>
void f(const C& container, // typename not allowed
typename C::iterator iter); // typename required
銆銆"typename must precede nested dependent type names"錛?#8220;typename 蹇呴』鍓嶇疆浜庡祵濂椾緷璧栫被鍨嬪悕”錛夎鍒欑殑渚嬪鏄?typename 涓嶅繀鍓嶇疆浜庡湪涓涓?list of base classes錛堝熀綾誨垪琛級涓殑鎴栬呭湪涓涓?member initialization list錛堟垚鍛樺垵濮嬪寲鍒楄〃錛変腑浣滀負涓涓?base classes identifier錛堝熀綾繪爣璇嗙錛夌殑 nested dependent type name錛堝祵濂椾緷璧栫被鍨嬪悕錛夈備緥濡傦細
class Derived: public Base<T>::Nested {
銆// base class list: typename not
銆public: // allowed
銆銆explicit Derived(int x)
銆銆: Base<T>::Nested(x) // base class identifier in mem
銆銆{
銆銆銆// init. list: typename not allowed
銆
銆銆銆typename Base<T>::Nested temp; // use of nested dependent type
銆銆銆 // name not in a base class list or
銆銆} // as a base class identifier in a
銆銆 // mem. init. list: typename required
};
銆銆璁╂垜浠潵鐪嬫渶鍚庝竴涓?typename 鐨勪緥瀛愶紝鍥犱負瀹冨湪浣犵湅鍒扮殑鐪熷疄浠g爜涓叿鏈変唬琛ㄦс傚亣璁炬垜浠湪鍐欎竴涓彇寰椾竴涓?iterator錛堣凱浠e櫒錛夌殑 function template錛堝嚱鏁版ā鏉匡級錛岃屼笖鎴戜滑瑕佸仛涓涓?iterator錛堣凱浠e櫒錛夋寚鍚戠殑 object錛堝璞★級鐨勫眬閮ㄦ嫹璐?temp錛屾垜浠彲浠ヨ繖鏍峰仛錛?br>
template<typename IterT>
void workWithIterator(IterT iter)
{
銆typename std::iterator_traits<IterT>::value_type temp(*iter);
銆
}
銆銆濡傛灉浣犺寰楄 std::iterator_traits<IterT>::value_type 浠や漢璁ㄥ帉錛屽氨鎯寵薄閭d釜涓庡畠鐩稿悓鐨勪笢瑗挎潵浠h〃瀹冦傚鏋滀綘鍍忓ぇ澶氭暟紼嬪簭鍛橈紝瀵瑰嬈¤緭鍏ュ畠鎰熷埌鎭愭儳錛岄偅涔堜綘灝遍渶瑕佸垱寤轟竴涓?typedef銆傚浜庡儚 value_type 榪欐牱鐨?traits member names錛堢壒鎬ф垚鍛樺悕錛夛紝涓涓氱敤鐨勬儻渚嬫槸 typedef name 涓?traits member name 鐩稿悓錛屾墍浠ヨ繖鏍風殑涓涓?local typedef 閫氬父瀹氫箟鎴愯繖鏍鳳細
template<typename IterT>
void workWithIterator(IterT iter)
{
銆typedef typename std::iterator_traits<IterT>::value_type value_type;
銆value_type temp(*iter);
銆
}
銆銆浣滀負緇撴潫璇紝鎴戝簲璇ユ彁鍙婄紪璇戝櫒涓庣紪璇戝櫒涔嬮棿瀵瑰洿緇?typename 鐨勮鍒欑殑鎵ц鎯呭喌鐨勪笉鍚屻備竴浜涚紪璇戝櫒鎺ュ彈蹇呴渶 typename 鏃跺畠鍗寸己澶辯殑浠g爜錛涗竴浜涚紪璇戝櫒鎺ュ彈涓嶈 typename 鏃跺畠鍗村瓨鍦ㄧ殑浠g爜錛涜繕鏈夊皯鏁扮殑錛堥氬父鏄佹棫鐨勶級浼氭嫆緇?typename 鍑虹幇鍦ㄥ畠蹇呴渶鍑虹幇鐨勫湴鏂廣傝繖灝辨剰鍛崇潃 typename 鍜?nested dependent type names錛堝祵濂椾緷璧栫被鍨嬪悕錛夌殑浜や簰浣滅敤浼氬鑷翠竴浜涜交寰殑鍙Щ妞嶆ч棶棰樸?br>
銆銆Things to Remember
銆銆·鍦ㄥ0鏄?template parameters錛堟ā鏉垮弬鏁幫級鏃訛紝class 鍜?typename 鏄彲浜掓崲鐨勩?br>
銆銆·鐢?typename 鍘繪爣璇?nested dependent type names錛堝祵濂椾緷璧栫被鍨嬪悕錛夛紝鍦?base class lists錛堝熀綾誨垪琛級涓垨鍦ㄤ竴涓?member initialization list錛堟垚鍛樺垵濮嬪寲鍒楄〃錛変腑浣滀負涓涓?base class identifier錛堝熀綾繪爣璇嗙錛夋椂闄ゅ銆?br>
]]>
渚嬪:
浣犲彲浠ュ0鏄庤繖鏍蜂竴涓瀯閫犲嚱鏁?/p>
class Array
{
public:
explicit Array(int size);
//
};
Array arr;
//
arr = 40;
闇瑕佹敞鎰忕殑鏄?explicit鍚屾牱涔熻兘闃繪"浠ヨ祴鍊艱娉曡繘琛屽甫鏈夎漿鍨嬫搷浣滅殑鍒濆鍖?;
渚嬪:
Array arr(40);//姝g‘
Array arr = 40;//閿欒
X x;
Y y(x);//鏄懼紡綾誨瀷杞崲
X x;
Y y = x;//闅愬紡綾誨瀷杞崲
explicit鍏抽敭瀛楃殑搴旂敤涓昏灝辨槸涓婇潰鎵璇寸殑鏋勯犲嚱鏁板畾涔変腑,鍙傝冭鍏抽敭瀛楃殑搴旂敤鍙互鐪嬬湅STL婧愪唬鐮?鍏朵腑澶ч噺浣跨敤浜嗚鍏抽敭瀛?
]]>
瀵瑰儚鐨勭姸鎬佷篃浼氶殢涔嬪彂鐢熷彉鍖?
濡傛灉涓涓被鐨勬垚鍛樺嚱鏁拌澹版槑涓篶onst綾誨瀷,琛ㄧず璇ュ嚱鏁頒笉浼氭敼鍙樺璞$殑鐘舵?涔熷氨鏄?br>璇ュ嚱鏁頒笉浼氫慨鏀圭被鐨勯潪闈欐佹暟鎹垚鍛?浣嗘槸鏈変簺鏃跺欓渶瑕佸湪璇ョ被鍑芥暟涓綾葷殑鏁版嵁鎴愬憳
榪涜璧嬪?榪欎釜鏃跺欏氨闇瑕佺敤鍒癿utable鍏抽敭瀛椾簡
渚嬪:
緙栬瘧涓婇潰鐨勪唬鐮佷細鍑虹幇 error C2166: l-value specifies const object鐨勯敊璇?br>璇存槑鍦╟onst綾誨瀷鐨勫嚱鏁頒腑鏀瑰彉浜嗙被鐨勯潪闈欐佹暟鎹垚鍛?
榪欎釜鏃跺欓渶瑕佷嬌鐢╩utable鏉ヤ慨楗頒竴涓嬭鍦╟onst鎴愬憳鍑芥暟涓敼鍙樼殑闈為潤鎬佹暟鎹垚鍛?br>m_nAccess,浠g爜濡備笅: