码迷,mamicode.com
首页 > 其他好文 > 详细

类模板成员函数

时间:2018-08-31 00:35:15      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:amp   编译   const   类模板   tchar   temp   code   编译器   getch   

#include <iostream>
using namespace std;


class Printer
{
public:
    template<typename T>//类的成员函数是模板
    void print(const T& t)
    {
        cout << t << endl;
    }

    template<typename T>
    void print(int a, const T& t)
    {
        cout << a << t << endl;
    }
};

int main()
{
    Printer p;
    p.print<const char*>("abc");                //类成员函数是模板的调用方式
    p.print("abc");                            //编译器推断参数模板类型
    p.print(2, "abc");                        //类模板成员函数重载
    getchar();
    return 0;
}

 

类模板成员函数

标签:amp   编译   const   类模板   tchar   temp   code   编译器   getch   

原文地址:https://www.cnblogs.com/zzyoucan/p/9563333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!