C++ 函数模板/类模板

#include <iostream>
#include <vector>
using namespace std;

template < class  T >  //类模板
class  Student
{
public:
    Student(T x)
    {
        this->a = x;
    }
    ~Student()
    {

    }
public:
    T a;
};

template<typename Y>   //函数模板

Y test(Y a, Y b)
{
    return a + b;
}

int main()
{

    Student<int> ttt1(1);                    //使用类模板
    Student<double>  ttt2(1.1);              //使用类模板 

    int    x = test<int>(1, 2);              //使用函数模板 
    double y = test<double>(1.1, 1.2);       //使用函数模板

    return 0;
}
函数模板格式
template<typename Y>   //函数模板
template < class  T >  //类模板

猜你喜欢

转载自www.cnblogs.com/shenji/p/12336734.html