64,636
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
template <typename T>
class demo{
protected:
T x;
public:
demo(T);
~demo();
void show();
};
template <typename T>
demo<T>::demo(T x):x(x){
cout << "demo<T>" << endl;
}
template <typename T>
demo<T>::~demo(){
}
template <typename T>
void demo<T>::show(){
cout << x << endl;
}
/*
template <>//对类进行全类特化,不过,我不想把成员函数写在类里面,于是我以部分特化的方式将成员函数写在外面。
class demo<int>{
protected:
int x;
public:
demo(int x);
~demo();
void show();
};
*/
template <>//将成员函数都特化在这里。
demo<int>::demo(int x):x(x){
cout << "demo<int>" << endl;
}
template <>
demo<int>::~demo(){
}
template <>
void demo<int>::show(){
cout << x+1 << endl;
}
int main(){
demo<double> d1(1.2);
demo<int> d2(2);
d1.show();
d2.show();
return 0;
}
template <>//是不是这里定义过后,下面就不用再重复定义了?
class demo<int>{
protected:
int x;
public:
demo(int x);
~demo();
void show();
};
//template <>
demo<int>::demo(int x):x(x){
cout << "demo<int>" << endl;
}
//template <>
demo<int>::~demo(){
}
//template <>
void demo<int>::show(){
cout << x+1 << endl;
}
[/quote]
是的
template <>//是不是这里定义过后,下面就不用再重复定义了?
class demo<int>{
protected:
int x;
public:
demo(int x);
~demo();
void show();
};
//template <>
demo<int>::demo(int x):x(x){
cout << "demo<int>" << endl;
}
//template <>
demo<int>::~demo(){
}
//template <>
void demo<int>::show(){
cout << x+1 << endl;
}