什么是函数模板和类模板(一个c程序有几个函数)

模板类与类模板、函数模板与模板函数等的区别函数指针=指向函数的指针指针函数=返回指针的函数数组指针=指向数组的指针指针数组=内容是指针的数组类模板=用来产生类的模板模板类=使用类模板产生的类函数模板=用来产生函数的模板模板函数=使用函数模板产生的函数后面转至https://www.cnblogs.com/wangduo/p/5559049.html …

大家好,又见面了,我是你们的朋友全栈君。

模板类与类模板、函数模板与模板函数等的区别

 

函数指针 = 指向函数的指针

指针函数=返回指针的函数

数组指针=指向数组的指针

指针数组=内容是指针的数组

类模板=用来产生类的模板

模板类=使用类模板产生的类

函数模板=用来产生函数的模板

模板函数=使用函数模板产生的函数

后面转至https://www.cnblogs.com/wangduo/p/5559049.html 

 在C++中有好几个这样的术语,但是我们很多时候用的并不正确,几乎是互相替换混淆使用。下面我想彻底辨清几个术语,这样就可以避免很多概念上的混淆和使用上的错误。这几个词是:  

  • 函数指针——指针函数
  • 数组指针——指针数组
  • 类模板——模板类
  • 函数模板——模板函数

  最终在使用中,我们就可以让它们实至名归,名正言顺。  

1.函数指针——指针函数  

  函数指针的重点是指针。表示的是一个指针,它指向的是一个函数,例子:

  int   (*pf)();  

  指针函数的重点是函数。表示的是一个函数,它的返回值是指针。例子:  

  int*   fun();  

2.数组指针——指针数组  

  数组指针的重点是指针。表示的是一个指针,它指向的是一个数组,例子:  

  int   (*pa)[8];  

  指针数组的重点是数组。表示的是一个数组,它包含的元素是指针。例子;  

  int*   ap[8];  

3.类模板——模板类(class   template——template   class)  

  类模板的重点是模板。表示的是一个模板,专门用于产生类的模子。例子:  

1 template <typename T> 2 class Vector 3 { 4  ... 5 };

  使用这个Vector模板就可以产生很多的class(类),Vector<int>、Vector<char>、 Vector<   Vector<int>  >、Vector<Shape*>……。  

  模板类的重点是类。表示的是由一个模板生成而来的类。例子:  

  上面的Vector<int>、Vector<char>、……全是模板类。  

  这两个词很容易混淆,我看到很多文章都将其用错,甚至一些英文文章也是这样。将他们区分开是很重要的,你也就可以理解为什么在定义模板的头文件.h时,模板的成员函数实现也必须写在头文件.h中,而不能像普通的类(class)那样,class的声明(declaration)写在.h文件中,class 的定义(definition)写在.cpp文件中。请参照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  

  我将几句关键的段落摘录如下,英文很好理解:  

  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: 

复制代码

1 template<class T> 2 class Foo { 3 public: 4  Foo(); 5 void someMethod(T x); 6 private: 7  T x; 8 };

复制代码

  Along with similar definitions for the member functions:   

复制代码

 1 template<class T>  2 Foo<T>::Foo()  3 {  4  ...  5 }  6  7 template<class T>  8 void Foo<T>::someMethod(T x)  9 { 10  ... 11 }

复制代码

  Now suppose you have some code in file Bar.cpp that uses Foo<int>:  

  //   Bar.cpp  

复制代码

1 void blah_blah_blah() 2 { 3   ... 4   Foo<int> f; 5   f.someMethod(5); 6   ... 7 } 

复制代码

  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().  

  关于一个缺省模板参数的例子:  

1 template <typename T = int> 2 class Array 3 { 4   ... 5 };

  第一次我定义这个模板并使用它的时候,是这样用的:  

  Array   books;//我认为有缺省模板参数,这就相当于Array<int>   books  

  上面的用法是错误的,编译不会通过,原因是Array不是一个类。正确的用法是Array<>   books;  

  这里Array<>就是一个用于缺省模板参数的类模板所生成的一个具体类。  

4.函数模板——模板函数(function   template——template   function)  

  函数模板的重点是模板。表示的是一个模板,专门用来生产函数。例子:  

1 template <typename T> 2 void fun(T a) 3 { 4   ... 5 }  

  在运用的时候,可以显式(explicitly)生产模板函数,fun<int>、fun<double>、fun<Shape*>……。  

  也可以在使用的过程中由编译器进行模板参数推导,帮你隐式(implicitly)生成。  

  fun(6);//隐式生成fun<int>  

  fun(8.9);//隐式生成fun<double>  

  fun(‘a’);//   隐式生成fun<char>  

  Shape*   ps   =   new   Cirlcle;  

  fun(ps);//隐式生成fun<Shape*>  

  模板函数的重点是函数。表示的是由一个模板生成而来的函数。例子:  

  上面显式(explicitly)或者隐式(implicitly)生成的fun<int>、fun<Shape*>……都是模板函数。  

  

  关于模板本身,是一个非常庞大的主题,要把它讲清楚,需要的不是一篇文章,而是一本书,幸运的是,这本书已经有了:David   Vandevoorde,   Nicolai   M.   Josuttis写的《C++ Templates:   The   Complete  Guide》。可惜在大陆买不到纸版,不过有一个电子版在网上流传。  

  模板本身的使用是很受限制的,一般来说,它们就只是一个产生类和函数的模子。除此之外,运用的领域非常少了,所以不可能有什么模板指针存在的,即指向模板的指针,这是因为在C++中,模板就是一个代码的代码生产工具,在最终的代码中,根本就没有模板本身存在,只有模板具现出来的具体类和具体函数的代码存在。  

  但是类模板(class   template)还可以作为模板的模板参数(template   template   parameter)使用,在Andrei   Alexandrescu的《Modern   C++   Design》中的基于策略的设计(Policy   based   Design)中大量的用到。  

1 template< typename T, template<typename U> class Y> 2 class Foo 3 { 4   ... 5 };  

  从文章的讨论中,可以看到,名字是非常重要的,如果对名字的使用不恰当的话,会引起很多的麻烦和误解。我们在实际的程序中各种标识符的命名也是一门学问,为了清晰易懂,有时候还是需要付出一定的代价。  

  最后提醒:在本文的几个术语中,语言的重心在后面,前面的词是作为形容词使用的。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/127868.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • hive中数据类型转换_csv文件导入sqlserver数据库中

    hive中数据类型转换_csv文件导入sqlserver数据库中1.类型映射关系mysql和hive中的数据类型存在差异,在mysql集成数据到hive中这样的场景下,我们希望在hive中的数据是贴源的,所以在hive中希望创建和mysql结构一致的表。mysql到hive数据类型映射参考如下:mysql数据类型hive数据类型整型bigintBIGINT整型intBIGINT整型smallintBIGINT整型tinyintBIGINT浮点型decimaldecimal浮点型double

    2022年9月21日
    0
  • 查看服务器外网ip

    查看服务器外网ip如果是桌面系统,想知道自己电脑的外网IP比较容易,用浏览器访问www.ip138.com,就可以了。而服务器放在机房,没有浏览器这号东西,就比较麻烦了。用traceroute又看不出来。偶然间,找到了一个方法可以查看服务器的外网IP。[javascript] viewplaincopy[zhou@localhost ~]$ wget htt

    2022年6月2日
    26
  • idea 2021.9最新激活码 mac[最新免费获取]

    (idea 2021.9最新激活码 mac)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~1STL…

    2022年3月27日
    64
  • Oracle的表空间quota详解[通俗易懂]

    转载至:http://czmmiao.iteye.com/blog/1291984表空间quota概述Oracle官网对quota的定义如下:Alimitonaresource,suchasalimitontheamountofdatabasestorageusedbyadatabaseuser.Adatabaseadministra

    2022年4月7日
    63
  • VB里的 dim是什么意思?[通俗易懂]

    VB里的 dim是什么意思?[通俗易懂]Dim为Dimension的缩写,后面加上所需变量的名字As为变量指定类型程序运行时,Dim语句就根据变量类型为变量分配内存空间

    2022年8月5日
    2
  • cas是乐观锁吗(java乐观锁)

    参考AtomicInteger类如下方法publicfinalintincrementAndGet(){for(;;){intcurrent=get();//1intnext=current+1;//2if(compareAndSet(current,next

    2022年4月10日
    51

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号