亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

問(wèn)模板函數(shù)、函數(shù)模板,模板類(lèi)、類(lèi)模板的區(qū)別的

系統(tǒng) 2121 0

問(wèn)模板函數(shù)、函數(shù)模板,模板類(lèi)、類(lèi)模板的區(qū)別的問(wèn)題? - 趙保龍 - 博客園

在C++中有好幾個(gè)這樣的術(shù)語(yǔ),但是我們很多時(shí)候用的并不正確,幾乎是互相替換混淆使用。下面我想徹底辨清幾個(gè)術(shù)語(yǔ),這樣就可以避免很多概念上的混淆和使用上的錯(cuò)誤。 ??
? ??
? 這幾個(gè)詞是: ??
? ??
? 函數(shù)指針——指針函數(shù) ??
? ??
? 數(shù)組指針——指針數(shù)組 ??
? ??
? 類(lèi)模板——模板類(lèi) ??
? ??
? 函數(shù)模板——模板函數(shù) ??
? ??
? 最終在使用中,我們就可以讓它們實(shí)至名歸,名正言順。 ??
? ??
? ? ??
? ??
? 1.函數(shù)指針——指針函數(shù) ??
? ??
? 函數(shù)指針的重點(diǎn)是指針。表示的是一個(gè)指針,它指向的是一個(gè)函數(shù),例子: ??
? ??
? int ? (*pf)(); ??
? ??
? 指針函數(shù)的重點(diǎn)是函數(shù)。表示的是一個(gè)函數(shù),它的返回值是指針。例子: ??
? ??
? int* ? fun(); ??
? ??
? ? ??
? ??
? 2.數(shù)組指針——指針數(shù)組 ??
? ??
? 數(shù)組指針的重點(diǎn)是指針。表示的是一個(gè)指針,它指向的是一個(gè)數(shù)組,例子: ??
? ??
? int ? (*pa)[8]; ??
? ??
? 指針數(shù)組的重點(diǎn)是數(shù)組。表示的是一個(gè)數(shù)組,它包含的元素是指針。例子; ??
? ??
? int* ? ap[8]; ??
? ??
? ? ??
? ??
? 3.類(lèi)模板——模板類(lèi)(class ? template——template ? class) ??
? ??
? 類(lèi)模板的重點(diǎn)是模板。表示的是一個(gè)模板,專(zhuān)門(mén)用于產(chǎn)生類(lèi)的模子。例子: ??
? ??
? template ? <typename ? T> ??
? ??
? class ? Vector ??
? ??
? { ??
? ??
? ? ? ? ? ? ? … ??
? ??
? }; ??
? ??
? 使用這個(gè)Vector模板就可以產(chǎn)生很多的class(類(lèi)),Vector<int>、Vector<char>、Vector< ? Vector<int> ? >、Vector<Shape*>……。 ??
? ??
? 模板類(lèi)的重點(diǎn)是類(lèi)。表示的是由一個(gè)模板生成而來(lái)的類(lèi)。例子: ??
? ??
? 上面的Vector<int>、Vector<char>、……全是模板類(lèi)。 ??
? ??
? 這兩個(gè)詞很容易混淆,我看到很多文章都將其用錯(cuò),甚至一些英文文章也是這樣。將他們區(qū)分開(kāi)是很重要的,你也就可以理解為什么在定義模板的頭文件.h時(shí),模板的成員函數(shù)實(shí)現(xiàn)也必須寫(xiě)在頭文件.h中,而不能像普通的類(lèi)(class)那樣,class的聲明(declaration)寫(xiě)在.h文件中,class的定義(definition)寫(xiě)在.cpp文件中。請(qǐng)參照Marshall ? Cline的《C++ ? FAQ ? Lite》中的[34] ? Container ? classes ? and ? templates中的[34.12] ? Why ? can't ? I ? separate ? the ? definition ? of ? my ? templates ? class ? from ? it's ? declaration ? and ? put ? it ? inside ? a ? .cpp ? file? ? URL地址是http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.12 ??
? ??
? 我將幾句關(guān)鍵的段落摘錄如下,英文很好理解: ??
? ??
? In ? order ? for ? the ? compiler ? to ? generate ? the ? code, ? it ? must ? see ? both ? the ? template ? definition ? (not ? just ? declaration) ? and ? the ? specific ? types/whatever ? used ? to ? "fill ? in" ? the ? template. ? For ? example, ? if ? you're ? trying ? to ? use ? a ? Foo<int>, ? the ? compiler ? must ? see ? both ? the ? Foo ? template ? and ? the ? fact ? that ? you're ? trying ? to ? make ? a ? specific ? Foo<int>. ? ??
? ??
? Suppose ? you ? have ? a ? template ? Foo ? defined ? like ? this: ? ??
? ??
? ? template<class ? T> ??
? ? class ? Foo ? { ??
? ? public: ??
? ? ? ? Foo(); ??
? ? ? ? void ? someMethod(T ? x); ??
? ? private: ??
? ? ? ? T ? x; ??
? ? }; ? ??
? ??
? Along ? with ? similar ? definitions ? for ? the ? member ? functions: ? ??
? ??
? ? template<class ? T> ??
? ? Foo<T>::Foo() ??
? ? { ??
? ? ? ? ... ??
? ? } ??
? ? ??
? ? template<class ? T> ??
? ? void ? Foo<T>::someMethod(T ? x) ??
? ? { ??
? ? ? ? ... ??
? ? } ? ??
? ??
? Now ? suppose ? you ? have ? some ? code ? in ? file ? Bar.cpp ? that ? uses ? Foo<int>: ? ??
? ??
? ? // ? Bar.cpp ??
? ? ??
? ? void ? blah_blah_blah() ??
? ? { ??
? ? ? ? ... ??
? ? ? ? Foo<int> ? f; ??
? ? ? ? f.someMethod(5); ??
? ? ? ? ... ??
? ? } ? ??
? ??
? Clearly ? somebody ? somewhere ? is ? going ? to ? have ? to ? use ? the ? "pattern" ? for ? the ? constructor ? definition ? and ? for ? the ? someMethod() ? definition ? and ? instantiate ? those ? when ? T ? is ? actually ? int. ? But ? if ? you ? had ? put ? the ? definition ? of ? the ? constructor ? and ? someMethod() ? into ? file ? Foo.cpp, ? the ? compiler ? would ? see ? the ? template ? code ? when ? it ? compiled ? Foo.cpp ? and ? it ? would ? see ? Foo<int> ? when ? it ? compiled ? Bar.cpp, ? but ? there ? would ? never ? be ? a ? time ? when ? it ? saw ? both ? the ? template ? code ? and ? Foo<int>. ? So ? by ? rule ? above, ? it ? could ? never ? generate ? the ? code ? for ? Foo<int>::someMethod(). ? ??
? ??
? 關(guān)于一個(gè)缺省模板參數(shù)的例子: ??
? ??
? template ? <typename ? T ? = ? int> ??
? ??
? class ? Array ??
? ??
? { ??
? ??
? ? ? ? ? ? ? … ??
? ??
? }; ??
? ??
? 第一次我定義這個(gè)模板并使用它的時(shí)候,是這樣用的: ??
? ??
? Array ? books;//我認(rèn)為有缺省模板參數(shù),這就相當(dāng)于Array<int> ? books ??
? ??
? 上面的用法是錯(cuò)誤的,編譯不會(huì)通過(guò),原因是Array不是一個(gè)類(lèi)。正確的用法是Array<> ? books; ??
? ??
? 這里Array<>就是一個(gè)用于缺省模板參數(shù)的類(lèi)模板所生成的一個(gè)具體類(lèi)。 ??
? ??
? ? ??
? ??
? 4.函數(shù)模板——模板函數(shù)(function ? template——template ? function) ??
? ??
? 函數(shù)模板的重點(diǎn)是模板。表示的是一個(gè)模板,專(zhuān)門(mén)用來(lái)生產(chǎn)函數(shù)。例子: ??
? ??
? template ? <typename ? T> ??
? ??
? void ? fun(T ? a) ??
? ??
? { ??
? ??
? ? ? ? ? ? ? … ??
? ??
? } ??
? ??
? 在運(yùn)用的時(shí)候,可以顯式(explicitly)生產(chǎn)模板函數(shù),fun<int>、fun<double>、fun<Shape*>……。 ??
? ??
? 也可以在使用的過(guò)程中由編譯器進(jìn)行模板參數(shù)推導(dǎo),幫你隱式(implicitly)生成。 ??
? ??
? fun(6);//隱式生成fun<int> ??
? ??
? fun(8.9);//隱式生成fun<double> ??
? ??
? fun(‘a(chǎn)’);// ? 隱式生成fun<char> ??
? ??
? Shape* ? ps ? = ? new ? Cirlcle; ??
? ??
? fun(ps);//隱式生成fun<Shape*> ??
? ??
? ? ??
? ??
? 模板函數(shù)的重點(diǎn)是函數(shù)。表示的是由一個(gè)模板生成而來(lái)的函數(shù)。例子: ??
? ??
? 上面顯式(explicitly)或者隱式(implicitly)生成的fun<int>、fun<Shape*>……都是模板函數(shù)。 ??
? ??
? 關(guān)于模板本身,是一個(gè)非常龐大的主題,要把它講清楚,需要的不是一篇文章,而是一本書(shū),幸運(yùn)的是,這本書(shū)已經(jīng)有了:David ? Vandevoorde, ? Nicolai ? M. ? Josuttis寫(xiě)的《C++ ? Templates: ? The ? Complete ? Guide》。可惜在大陸買(mǎi)不到紙版,不過(guò)有一個(gè)電子版在網(wǎng)上流傳。 ??
? ??
? ? ??
? ??
? 模板本身的使用是很受限制的,一般來(lái)說(shuō),它們就只是一個(gè)產(chǎn)生類(lèi)和函數(shù)的模子。除此之外,運(yùn)用的領(lǐng)域非常少了,所以不可能有什么模板指針存在的,即指向模板的指針,這是因?yàn)樵贑++中,模板就是一個(gè)代碼的代碼生產(chǎn)工具,在最終的代碼中,根本就沒(méi)有模板本身存在,只有模板具現(xiàn)出來(lái)的具體類(lèi)和具體函數(shù)的代碼存在。 ??
? ??
? 但是類(lèi)模板(class ? template)還可以作為模板的模板參數(shù)(template ? template ? parameter)使用,在Andrei ? Alexandrescu的《Modern ? C++ ? Design》中的基于策略的設(shè)計(jì)(Policy ? based ? Design)中大量的用到。 ??
? ??
? template< ? typename ? T, ? template<typename ? U> ? class ? Y> ??
? ??
? class ? Foo ??
? ??
? { ??
? ??
? ? ? ? ? ? ? … ??
? ??
? }; ??
? ??
? ? ??
? ??
? 從文章的討論中,可以看到,名字是非常重要的,如果對(duì)名字的使用不恰當(dāng)?shù)脑挘瑫?huì)引起很多的麻煩和誤解。我們?cè)趯?shí)際的程序中各種標(biāo)識(shí)符的命名也是一門(mén)學(xué)問(wèn),為了清晰易懂,有時(shí)候還是需要付出一定的代價(jià)。 ??
? ??
? 最后提醒:在本文的幾個(gè)術(shù)語(yǔ)中,語(yǔ)言的重心在后面,前面的詞是作為形容詞使用的。 ??
? ??
? 吳桐寫(xiě)于2003.6.1 ??
? ??
? 最近修改2003.6.16 ??
? 這個(gè)是一個(gè)哥們?cè)贑SDN上寫(xiě)的東東,呵呵 ??
? http://www.csdn.net/develop/read_article.asp?id=19075

問(wèn)模板函數(shù)、函數(shù)模板,模板類(lèi)、類(lèi)模板的區(qū)別的問(wèn)題?


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 亚洲欧洲视频在线 | 99久久久久国产精品免费 | 久久午夜夜伦伦鲁鲁片 | 黄色成人免费观看 | 亚洲欧美日韩一区成人 | 午夜精品成年片色多多 | 日本大胆一区免费视频 | 亚洲国产人成中文幕一级二级 | 中文字幕在线一区二区在线 | 久草视频在线看 | 奇米色第四色 | 午夜亚洲国产精品福利 | 高清一区二区三区免费 | 男人搡女人视频免费看 | 亚洲欧洲国产精品你懂的 | 91手机看片国产永久免费 | 国产欧美成人xxx视频 | 伊人三区| 国产在线日韩 | 国产福利小视频在线观看 | 国产精品久久久久尤物 | 国产欧美一区二区三区免费 | 国内精自线一二区 | 日本不卡视频免费的 | 久久国产精品永久免费网站 | 亚洲高清一区二区三区四区 | 日本免费的一级v一片 | 播放一级片 | 国产视频一区二区三区四区 | 综合图区亚洲白拍在线 | 国产精品tv | 成人免费视频一区二区 | 羞羞视频免费网站在线 | 免费欧美黄色网址 | 五月天丁香六月欧美综合 | 日日摸日日添日日透 | 在线日本妇人成熟免费观看 | 久久久久久国产精品免费免 | 四虎精品永久在线 | 久久久夜色精品国产噜噜 | 又刺激又黄的一级毛片 |