首页 > 编程知识 正文

函数模板特化,如何调用类中的函数模板

时间:2023-05-06 03:08:50 阅读:24596 作者:78

译文: https://blog.csdn.net/JFK idear/article/details/24656929? UTM _ medium=distribute.PC _ relevant.none-task-blog-searchfrombaidu-6.control dist _ request _ id=1328655306;

3359 exp.newsmth.net/topic/9e d 77 d7bfcb 8169 d 011 C2 e D5 ab 3259 d 7

今天我想为不同的指针类型写一系列处理函数,但是对于该函数的重载,同名函数的重载写得一大堆,看到类声明就觉得非常难看。 于是考虑了模板和特化,测试了两个代码。

别胡说,贴上代码。

示例1 :类模板的成员函数特化

这里专门设计的是班级

templateclasstclasssample { public : void print (} { printf ('nprinttemplate ' ); }; voidsampleint :3360打印((printf ) (nprintint ); (; //int_tmain(intargc,_ tchar * argv [ ]; a.print (; 样品双面b; b .打印(; 返回0; //输出/*打印int打印模板* /示例2 :类的成员模板函数专用

这里,只有特化函数

class sample2{ public : templateclasstvoidprint (} { printf ('n sample 2打印模板' } ); }; templatevoidsample 2:3360打印int ((printf ) (n sample 2打印int ); //int _ tmain (调用int argc,_TCHAR* argv[] ) {Sample2 sam2; Sam2.打印双精度(; Sam2.打印int (; 返回0; //输出//* sample2printtemplatesample2print int//https://www.dazhu anlan.com/2020/03/24/5e 798 f 3d 451 F6 /这尤其好

如果存在重定义问题: https://www.cn blogs.com/Roy muste/archive/2013/05/04/3059428.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。