一、代码
#include <iostream>
using namespace std;
//嵌入的类型信息
template <typename T>
class Array
{
public:
Array(T num) : m_num(num) { }
T get_num() { return m_num; }
typedef T Elem; //元素的类型
typedef T Temp; //临时对象的类型
private:
T m_num;
};
template <typename Container>
typename Container::Elem process(Container& c)
{
//
typename Container::Temp tmp;
tmp = c.get_num();
return tmp;
}
int main(int argc, char* argv[])
{
//
Array<int> arr1(20);
cout<<process(arr1)<<endl;
//
Array<string> arr2("hello world");
cout<<process(arr2)<<endl;
return 0;
}
二、输出结果