首页 > 编程语言 > 详细

【C++ Primer 第16章】重载与模板

时间:2021-06-28 09:38:03      阅读:32      评论:0      收藏:0      [点我收藏+]

 

编写重载模板

 1 #include <iostream>
 2 #include <sstream>
 3 #include <string>
 4 using namespace std;
 5 
 6 template <typename T>
 7 string debug_rep(const T &t)
 8 {
 9   ostringstream ret;
10   ret << t;
11   return ret.str();
12 }
13 
14 template <typename T>
15 string debug_rep(T *p)
16 {
17   ostringstream ret;
18   ret << "point: " << p;
19   if (p)
20     ret << " " << debug_rep(*p);
21   else
22     ret << " null pointer";
23 
24   return ret.str();
25 }
26 
27 int main()
28 {
29   string s("hi");
30   cout << debug_rep(s) << endl;
31   cout << debug_rep(&s) << endl;
32 
33   const string *sp = &s;
34   cout << debug_rep(sp) << endl;
35   return 0;
36 }

输出:

技术分享图片

 

 

2.

【C++ Primer 第16章】重载与模板

原文:https://www.cnblogs.com/sunbines/p/14942154.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!