c/c++语言开发共享类模版与函数模版

“` // // main.cpp // 类模版与函数模版 // 在类声明中使用类型参数来声明通用的类 // Created by mac on 2019/4/6. // Copyright © 2019年 mac. All rights reserved. // 一个类模版中是否支持多种类型的参 …

// //  main.cpp //  类模版与函数模版 //  在类声明中使用类型参数来声明通用的类 //  created by mac on 2019/4/6. //  copyright © 2019年 mac. all rights reserved. //  一个类模版中是否支持多种类型的参数?--可以的 // 在algorithm下已经写过swap函数了,所以自己再用swap这个免费精选名字大全写的就通过不了  #include <iostream> #include <string> //#include <algorithm> using namespace std;  template <class gentype,int size=50> class genclass { public:     genclass(){         cout<<"类模版中执行构造函数"<<endl;     }     ~genclass(){         cout<<"类模版中执行析构函数"<<endl;     }     gentype storage[size]; };  template <class gentype> void swap(gentype &el1,gentype &el2) {     gentype tmp=el1;el1=el2;el2=tmp; }  int main(int argc, const char * argv[]) {     int a=1,b=2;     float c=3.3,d=4.4;     //使用类模版定义对象     genclass<int> intobject1;     genclass<int,100> intobject2;     genclass<float,123> floatobject;     swap(c,d);     cout<<c<<"t"<<d<<endl;     return 0; } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/604381.html

(0)
上一篇 2021年5月12日
下一篇 2021年5月12日

精彩推荐