【C++编程】重载与模板

 

编写重载模板

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

template <typename T>
string debug_rep(const T &t)
{
  ostringstream ret;
  ret << t;
  return ret.str();
}

template <typename T>
string debug_rep(T *p)
{
  ostringstream ret;
  ret << "point: " << p;
  if (p)
    ret << " " << debug_rep(*p);
  else
    ret << " null pointer";

  return ret.str();
}

int main()
{
  string s("hi");
  cout << debug_rep(s) << endl;
  cout << debug_rep(&s) << endl;

  const string *sp = &s;
  cout << debug_rep(sp) << endl;
  return 0;
}

 

输出:

 

 

2.

posted @ 2021-06-27 21:15  苏格拉底的落泪  阅读(36)  评论(0编辑  收藏  举报