#include <iostream>
using namespace std;
class A {
public:
template<typename T> void display(T temp);
template<typename T> A(T temp);
};
template<typename T>
void A::display(T temp)
{
cout<<temp<<endl;
}
template<typename T>
A::A(T temp)
{
cout<<temp<<endl;
}
template <typename T>
void test(T temp)
{
cout<<temp<<endl;
}
void main()
{
test<int>(12); //普通模板函数,在VS2013下测试,其实加不加后面的<int>都可
A aa(12); //请注意这一行
aa.display<int>(15); //成员函数是模板函数
}
(1) 要注意的 地方是 函数的实现和申明要写在同一个文件里面,也是说如果类的申明是在 **.h文件里面,那么函数的实现也要在同一个**.h文件里面。
本文深入探讨了C++中模板函数的应用,特别是如何在类中使用模板成员函数,并提供了具体的代码示例。强调了模板函数的实现和声明需在同一文件中的重要性。
590

被折叠的 条评论
为什么被折叠?



