Template

上传人:油条 文档编号:13410687 上传时间:2017-10-24 格式:DOC 页数:6 大小:58.50KB
返回 下载 相关 举报
Template_第1页
第1页 / 共6页
Template_第2页
第2页 / 共6页
Template_第3页
第3页 / 共6页
Template_第4页
第4页 / 共6页
Template_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《Template》由会员分享,可在线阅读,更多相关《Template(6页珍藏版)》请在金锄头文库上搜索。

1、Templates定义一个模板使用的关键字:class 和 typename。在定义作为模板的时候:class 和 typename,作用是一样的。一 函数模板1 定义方式template function_declaration;template function_declaration;2 Exampletemplate T GetMax (T a, T b) T result;result = (ab)? a : b;return (result);3 使用方法function_name (parameters);int x,y;GetMax (x,y);对于内部类型,通常是可以无需制定

2、具体的类型,编译器会自动识别,而是写成:int x,y;GetMax(x,y); /x,y 为相同的类型int x;long y;GetMax(x,y); /x,y 为不相同的类型 报错 两种不同的内部类型必须:template T GetMin (T a, U b) return (a (j,l); /或者 i = GetMin (j,l);二 Class Template通常是使用模板作为类成员变量的类型1 Example:template class mypair T values 2;public:mypair (T first, T second) /内联函数values0=firs

3、t; values1=second;mypair myobject (115, 36); /这样使用mypair myfloats (3.0, 2.18);2 成员函数为非内联函数template class mypair T a, b;public:mypair (T first, T second)a=first; b=second;T getmax ();template /增加此声明T mypair:getmax ()T retval;retval = ab? a : b;return retval;int main () mypair myobject (100, 75);cout

4、T mypair:getmax ()三 模板特化 Template Specialization1 将模板类转化为特定类型相关的类/ class template:template class mycontainer T element;public:mycontainer (T arg) element=arg;T increase () return +element;/ class template specialization:template /将其特例化为 char 型char element;public:mycontainer (char arg) element=arg;ch

5、ar uppercase ()if (element=a)&(element myint (7);mycontainer mychar (j);cout class mycontainer . ; /模板类template . ; /特例化为 char 型template . ; /特例化为 int 型模板来与之相应的特例化的类之间,并不是什么继承关系,所以在特例化一个模板类时,需要将其所有的包含成员进行重写,可以进行扩展。2 非模板类型参数 Non-type parameters for templatestemplate /int Nclass mysequence T memblock

6、N;public:void setmember (int x, T value);T getmember (int x);template void mysequence:setmember (int x, T value) memblockx=value;template T mysequence:getmember (int x)return memblockx;int main () mysequence myints;mysequence myfloats;myints.setmember (0,100);myfloats.setmember (3,3.1416);cout class

7、 mysequence .;mysequence myseq; /等同于上面四 Templates and multiple-file projectsFrom the point of view of the compiler, templates are not normal functions or classes. They are compiled on demand, meaning that the code of a template function is not compiled until an instantiation with specific template a

8、rguments is required. At that moment, when an instantiation is required, the compiler generates a function specifically for those arguments from the template.模板不会在代码编译的时候直接进行编译,而是在被实例化一个特定类型的模板时候,才会编译到,生成一个特定参数的函数。When projects grow it is usual to split the code of a program in different source code

9、 files. In these cases, the interface and implementation are generally separated. Taking a library of functions as example, the interface generally consists of declarations of the prototypes of all the functions that can be called. These are generally declared in a header file with a .h extension, a

10、nd the implementation (the definition of these functions) is in an independent file with c+ code.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 declaratio

11、n. 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 pr

12、epared to allow the inclusion more than once of the same template file with both declarations and definitions in a project without generating linkage errors.二 typename定义一个模板使用的关键字:class 和 typename。在定义作为模板的时候:class 和 typename,作用是一样的。使用 typename,实际上也是为模板服务的,在某些情况下我们需要明确的指出某些变量为模板。1 在一个模板类中使用另一个模板类定义一个

13、变量时templatestruct first typedef T * pointer;templateclass second first:pointer p; / syntax error (VC+中 并不报错);In a template, the name of a member of another class that depends on its template parameter(s) (first:pointer in this example, dependent on the T parameter) is a dependent name that is not looked-up immediately. To tell the compiler that it is meant to refer to a type and not some other sort of member, you must add the keyword typename before it.(英文太烂,就不翻译了)typename 能够在:依赖类型中, typename 用来声明一个依赖于另一个模板类参数的嵌套类型。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号