首页 > 编程语言 > 详细

C++ 函数模板/类模板

时间:2020-02-20 19:35:06      阅读:96      评论:0      收藏:0      [点我收藏+]
#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 >  //类模板

 

C++ 函数模板/类模板

原文:https://www.cnblogs.com/shenji/p/12336734.html

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