C++编程思想:模板
生活随笔
收集整理的這篇文章主要介紹了
C++编程思想:模板
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 模板的特性1:需要遵守約定
- 模板類型的模板參數(shù)
- 函數(shù)成員模板和類成員模板
- 模板特化以及模板重載
- 模板中的符號(hào)解析
模板的特性1:需要遵守約定
模板將類型以一個(gè)標(biāo)識(shí)符替代,想要使用模板就需要遵守模板對(duì)這個(gè)標(biāo)識(shí)符的要求,比如對(duì)標(biāo)識(shí)符有什么成員變量以及成員函數(shù)。
#include <iostream> #include <vector> using namespace::std;//*****************這是一個(gè)模板類的實(shí)現(xiàn),展示模板的特性之一:約定性******************************* template<class T> class X { public:typename T::id i; //約定類型T中有id這個(gè)類型,并且創(chuàng)建一個(gè)id類型的變量,typename是關(guān)鍵字,用typename 告訴編譯器T::id是一個(gè)類型標(biāo)識(shí)符 public:void f() {i.g(); //約定i中有g(shù)()這個(gè)函數(shù)} };class Y { public:class id{public:void g() {cout << "this is the Y::id object function g()" << endl;}}; };/* int main() {X<Y> xy;xy.f(); } */模板類型的模板參數(shù)
//********************************這是一個(gè)模板類型的模板參數(shù)案例**************************** template <class T, template <class> class Seq> //T是一個(gè)類型,Seq是一個(gè)模板類型,這個(gè)模板類型有一個(gè)參數(shù)(無(wú)需寫出參數(shù)名) class Container { public:Seq<T> seq; //Seq模板類型中的類型參數(shù)是T public:void printSeq() {cout << "the size of seq : " << seq.size() << endl;}void push_back(T t){seq.push_back(t);} }; template<class T> class Vec { public:vector<T> vec; public:void push_back(T t){vec.push_back(t);}size_t size() {return vec.size();} };/* int main() {Container<int, Vec> cv;cv.push_back(1);cv.push_back(2);cv.printSeq();} */函數(shù)成員模板和類成員模板
//***********************成員函數(shù)中的模板:分為函數(shù)成員模板和類成員模板********************************* template <class T> class com { public:T t;com(const T& tt) :t(tt){std::cout << "t : " << t << endl;}template <class U> com(const com<U>& u) : t(u.t) //成員函數(shù)模板{std::cout << "u : " << t << endl;} };template <class T> class outer { public:template<class R> class iner //成員函數(shù)類{public://template<class T> template<class R>void f(){std::cout << "out " << typeid(T).name() << endl;std::cout << "inner " << typeid(R).name() << endl;std::cout << "this " << typeid(*this).name() << endl;}}; };/* int main() {com<float> a(1);com<double> b(a);outer<float>::iner<int> ou;ou.f();outer<float>::iner<int> oc(ou); //這里會(huì)調(diào)用拷貝構(gòu)造函數(shù) }*/模板特化以及模板重載
在實(shí)例化模板時(shí),優(yōu)先考慮特化程度高的模板,比如有指針修飾符修飾的模板或者直接特化的模板,若特化程度高的模板可以匹配就使用特化程度高的模板,當(dāng)然匹配度是更重要的指標(biāo)。
//*****************模板特化以及模板重載************************************ #include<stack> template <class StackType> void emptyTheStack(StackType& stk) {cout << "this is stackType" << endl; }template <class StackType> void emptyTheStack(stack<StackType*> & stk) //經(jīng)過指針特化的模板,當(dāng)實(shí)例化為參數(shù)為指針時(shí)優(yōu)先調(diào)用此模板 {cout << "this is stack<StackType*>" << endl; }template<> void emptyTheStack(float& stk) //直接float 類型,顯示特化 {cout << "this is float& stk" << endl; }void emptyTheStack(int stk) //重載模板函數(shù) {cout << "this is int stk" << endl; }int main() {double a = 1;emptyTheStack(a); //調(diào)用第一個(gè)模板float b = 1;emptyTheStack(b);//調(diào)用第三個(gè)模板stack<float*> c;emptyTheStack(c); //調(diào)用第二個(gè)模板int d = 1;emptyTheStack(d);//調(diào)用最后一個(gè)重載函數(shù) }模板中的符號(hào)解析
如果符號(hào)定義涉及到未確定類型,在編譯的第一階段只會(huì)在已經(jīng)定義的符號(hào)表中查找該符號(hào),不會(huì)到不確定類型中查找該符號(hào)。
//********************************關(guān)聯(lián)不確定類型標(biāo)識(shí)符的編譯************************* #include<algorithm> #include<typeinfo> using std::cout; using std::endl;void g() { cout << "global g()" << endl; } //全局定義template <class T> class YY { public :void g(){cout << "Y<" << typeid(T).name() << ">::g()" << endl;}void h(){cout << "Y<" << typeid(T).name() << ">::h()" << endl;}typedef int E; //局部定義 };typedef double E; //全局定義//一個(gè)全局定義的模板函數(shù) template<class T> void swap(T& t1, T& t2) {cout << "global swap" << endl;T temp = t1;t1 = t2;t2 = temp; }template<class T> class XX :public YY<T> { public:E f() //E的返回類型是double{g(); //這個(gè)會(huì)編譯成全局函數(shù),而不是YY中的函數(shù)定義體,因?yàn)閅Y中的函數(shù)定義體是關(guān)聯(lián)未定類型的,不會(huì)優(yōu)先編譯this->h();//這是YY中的T t1 = T(0);T t2 = T(1);cout << "t1: " << t1 << endl;cout << "t2: " << t2 << endl;swap(t1, t2);std::swap(t1, t2);cout << "E type: " << typeid(E).name() << endl;return E(t2);} }; int main() {XX<int> x;cout << x.f() << endl; }總結(jié)
以上是生活随笔為你收集整理的C++编程思想:模板的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++编程思想:继承与虚函数以及多态
- 下一篇: C++编程思想:C++string