标签:理解 应该 接受 条件 数据 double 特性 声明 子类
#include <iostream> #include <typeinfo> using namespace std; template<typename A = int,typename B = double, typename C = string> //template<typename A,typename B = double, typename C = string> //ok //template<typename A = int,typename B, typename C = string> //error //template<typename A = int,typename B = double, typename C> //error class X { public: static void foo (void) { cout << typeid (A).name () << ‘ ‘ << typeid (B).name () << ‘ ‘ << typeid (C).name () << endl; } }; int _x = 200; void foo (int x, int y = /*x*/_x) { cout << x << ‘ ‘ << y << endl; } template<typename A, typename B = A> //template<typename A = B, typename B = int> //error class Y { public: static void foo (void) { cout << typeid (A).name () << ‘ ‘ << typeid (B).name () << endl; } }; template<typename A = int, typename B = double, typename C = string> void fun (void) { cout << typeid (A).name () << ‘ ‘ << typeid (B).name () << ‘ ‘ << typeid (C).name () << endl; } template<typename T = int> void bar (T arg) { cout << typeid (arg).name () << endl; } int main (void) { X<char, short, long>::foo (); // c s l X<char, short>::foo (); // c s Ss X<char>::foo (); // c d Ss // X<>::foo (); // i d Ss foo (100); // PUSH ... 100 // PUSH ... _x Y<long long>::foo (); // Y<>::foo (); fun<char, short, long> (); fun<char, short> (); fun<char> (); fun<> (); fun (); bar (1.23); return 0; }
#include <iostream> #include <iomanip> using namespace std; template<typename T = int, size_t S = 3> class Array { public: T& operator[] (size_t i) { return m_a[i]; } T const& operator[] (size_t i) const { return const_cast<Array&> (*this)[i]; } T m_a[S]; }; int square (int x) { return x * x; } template<int x> int square (void) { return x * x; } /* int square (void) { return 100;} */ int main (void) { Array<int> a; /* a.m_a[0] = 10; a.m_a[1] = 20; a.m_a[2] = 30; */ a[0] = 10; // a.operator[] (0) = 10; a[1] = 20; a[2] = 30; Array<int> const b = a; // cout << b.m_a[0] << ‘ ‘ << b.m_a[1] << ‘ ‘ << b.m_a[2] << endl; cout << b[0] << ‘ ‘ << b[1] << ‘ ‘ << b[2] << endl; Array<string> c; c[0] = "北京"; c[1] = "上海"; c[2] = "广州"; cout << c[0] << ‘ ‘ << c[1] << ‘ ‘ << c[2] << endl; int const /*volatile */x = 2, y = 1; Array<Array<int, x+y+1>, 1+1+1> d; // Array<Array<int, 2+1+1>, 1+1+1> d; for (int i = 0; i < 3; ++i) for (int j = 0; j < 4; ++j) d[i][j] = i * 4 + j + 1; // d.operator[](i).operator[](j) = ...; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 4; ++j) cout << setw (2) << d[i][j] << ‘ ‘; cout << endl; } Array<Array<Array<int> > > e; cout << square (10) << endl; cout << square<10> () << endl; return 0; }
class关键字:1)声明类 2)声明模板的类型实参typename关键字:1)解决嵌套依赖 2)声明模板的类型参数struct关键字:声明类
#include <iostream> using namespace std; class A { public: typedef unsigned int uint; class B {}; }; template<typename T> void foo (void) { typename T::uint u; typename T::B b; } void bar (void) {} int main (void) { A::uint u; A::B b; foo<A> (); return 0; }
#include <iostream> #include <typeinfo> using namespace std; template<typename T> class A { public: // 嵌套模板函数 template<typename U> void foo (void) const { U var; cout << typeid (m_var).name () << ‘ ‘ << typeid (var).name () << endl; } private: T m_var; }; template<typename T> void bar (T const& a, T const* b) { a.template foo<double> (); b->template foo<double> (); } int main (void) { A<int> a; a.foo<double> (); bar (a, &a); return 0; }
#include <cstdlib> #include <iostream> using namespace std; class A { public: int m_var; void foo (void) const {} class X {}; }; class B : public A { public: void bar (void) const { cout << /*A::*/m_var << endl; /*A::*/foo (); /*A::*/X x; } }; template<typename T> class C { public: int m_var; void foo (void) const {} class X {}; void exit (int status) const { cout << "再见!" << endl; } }; template<typename T> class D : public C<T> { public: void bar (void) const { // cout << C<T>::m_var << endl; cout << this->m_var << endl; // C<T>::foo (); this->foo (); typename C<T>::X x; // C<T>::exit (0); this->exit (0); } }; int main (void) { B b; b.bar (); D<int> d; d.bar (); return 0; }
#include <iostream> using namespace std; template<typename T> class Array { public: void pushBack (T const& data) { cout << "向数组尾端压入数据" << endl; } void popBack (void) { cout << "从数组尾端弹出数据" << endl; } }; template<typename T> class List { public: void pushBack (T const& data) { cout << "向链表尾端压入数据" << endl; } void popBack (void) { cout << "从链表尾端弹出数据" << endl; } }; template<typename T,template<typename> class C> class Stack { public: void push (T const& data) { m_c.pushBack (data); } void pop (void) { m_c.popBack (); } private: C<T> m_c; }; int main (void) { Stack<int, Array> sia; sia.push (100); sia.pop (); Stack<int, List> sil; sil.push (200); sil.pop (); return 0; }
#include <iostream> using namespace std; template<typename T> void foo (void) { T var = T (); // int var = int (); // string var = string (); // Integer var = Integer (); cout << ‘[‘ << var << ‘]‘ << endl; } template<typename T> class Foo { public: Foo (void) : m_var () {} T m_var; }; class Integer { public: Integer (int arg = 0) : m_var (arg) {} friend ostream& operator<< (ostream& os,Integer const& i) { return os << i.m_var; } private: int m_var; }; int main (void) { /* foo<int> (); foo<string> (); foo<Integer> (); */ Foo<int> fi; cout << ‘[‘ << fi.m_var << ‘]‘<< endl; Foo<string> fs; cout << ‘[‘ << fs.m_var << ‘]‘ << endl; Foo<Integer> fn; cout << ‘[‘ << fn.m_var << ‘]‘ << endl; return 0; }
不能被声明为虚函数的函数:全局函数,静态成员函数,构造函数,模板性成员函数
#include <iostream> using namespace std; template<typename A, typename B> class X { public: // 模板中的虚函数 virtual A foo (B arg) const { cout << "X::foo" << endl; return A (); } // 不可以声明虚模板函数 /* template<typename C> virtual void bar (void) const {} */ }; template<typename A, typename B, typename C, typename D> class Y : public X<C, D> { public: A foo (B arg) const { cout << "Y::foo" << endl; return A (); } }; int main (void) { Y<int, double, int, double> y1; X<int, double>& x1 = y1; x1.foo (1.2); Y<int, double, int, char> y2; X<int, char>& x2 = y2; x2.foo (‘A‘); /* Y<int, double, char, double> y3; X<char, double>& x3 = y3; x3.foo (1.2); */ return 0; }
标签:理解 应该 接受 条件 数据 double 特性 声明 子类
原文地址:http://www.cnblogs.com/cotsnail/p/6158896.html