标签:system class 不能 std 转化 main his type --
#include<iostream> #include<string> using namespace std; template < typename T>//定义模版不能加分号 class A { public: A(T a) { this->a = a; } T &get() { return a; } private: T a; }; int main() { A<int> a1(10); // 这里必须写上<>里面的内容! cout << a1.get() << endl; A<char> a2(‘l‘); cout << a2.get() << endl; A<string > a3("love"); string temp=a3.get() ; cout << temp << endl; system("pause"); }
#include<iostream> using namespace std; template <typename T> void Staff(T a, T b) { cout << "a= " << a << " b = " << b << endl; cout << "我只是一个普通的函数模版、\n"; } void Staff(int a, char b) { cout << "a= " << a << " b = " << b << endl; cout << "我只是一个普通的函数\n"; } int main() { int x = 101; char y = ‘a‘; int z = 100; Staff(x, y);//调用的普通函数 Staff(y, x); 调用的普通函数,普通函数可以自动类型转化 Staff(x, x); 调用的函数模版 函数模版不可以自动类型的转化 system("pause"); }
标签:system class 不能 std 转化 main his type --
原文地址:http://www.cnblogs.com/xiaochige/p/6705857.html