Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360651
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-19 12:51:02

Format:
  1. template <class identifier> function_declaration;
  2. template <typename identifier> function_declaration;
Use:
  1. function_name <type> (parameters);
Example:
  1. #include <iostream>
  2. using namespace std;

  3. template <class T>
  4. T GetMax (T a, T b)
  5. {
  6.         T result;
  7.         result = (a > b) ? a : b;
  8.         return (result);
  9. }

  10. int
  11. main(void)
  12. {
  13.         int i=5, j=6, k;
  14.         long l=10, m=5, n;
  15.         k = GetMax<int>(i,j);
  16.         n = GetMax<long>(l,m);

  17.         cout << k << endl;
  18.         cout << n << endl;

  19.         return (0);
  20. }

Class template:
  1. #include <iostream>
  2. using namespace std;

  3. template <class T>
  4. class mypair {
  5.                 T a, b;
  6.         public:
  7.                 mypair (T first, T second) {
  8.                         a = first;
  9.                         b = second;
  10.                 }
  11.                 T getmax();
  12. };

  13. template <class T>
  14. T mypair<T>::getmax()
  15. {
  16.         T retval;
  17.         retval = a > b ? a : b;
  18.         return retval;
  19. }

  20. int
  21. main(void)
  22. {
  23.         mypair<int> myobject (100, 75);
  24.         cout << myobject.getmax() << endl;

  25.         return (0);
  26. }

Template specialization:
  1. #include <iostream>
  2. using namespace std;

  3. template <class T>
  4. class mycontainer {
  5.                 T element;
  6.         public:
  7.                 mycontainer (T arg) {element = arg;}
  8.                 T increase () {return (++element);}
  9. };

  10. template <>
  11. class mycontainer<char> {
  12.                 char element;
  13.         public:
  14.                 mycontainer (char arg) {element = arg;}
  15.                 char uppercase() {
  16.                         if ((element >= 'a') && (element <= 'z'))
  17.                                 element += 'A'-'a';
  18.                         return (element);
  19.                 }
  20. };

  21. int
  22. main(void)
  23. {
  24.         mycontainer<int> myint (7);
  25.         mycontainer<char> mychar ('j');
  26.         cout << myint.increase() << endl;
  27.         cout << mychar.uppercase() << endl;

  28.         return (0);
  29. }

  1. #include <iostream>
  2. using namespace std;

  3. template <class T, int N>
  4. class mysequence {
  5.                 T memblock [N];
  6.         public:
  7.                 void setmember (int x, T value);
  8.                 T getmember (int x);
  9. };

  10. template <class T, int N>
  11. void mysequence<T, N>::setmember(int x, T value)
  12. {
  13.         memblock[x] = value;
  14. }

  15. template <class T, int N>
  16. T mysequence<T, N>::getmember(int x)
  17. {
  18.         return memblock[x];
  19. }

  20. int
  21. main(void)
  22. {
  23.         mysequence<int, 5> myints;
  24.         mysequence<double, 5> myfloats;
  25.         myints.setmember(0,100);
  26.         myfloats.setmember(3, 3.1416);
  27.         cout << myints.getmember(0) << endl;
  28.         cout << myfloats.getmember(3) << endl;

  29.         return (0);
  30. }
Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.

Since no code is generated until a template is instantiated when required, compilers are prepared to allow the inclusion more than once of the same template file with both declarations and definitions in a project without generating linkage errors.

阅读(856) | 评论(0) | 转发(0) |
0

上一篇:多态

下一篇:名字空间

给主人留下些什么吧!~~