For investors
股价:
5.36 美元 %For investors
股价:
5.36 美元 %认真做教育 专心促就业
模板类必须所有的方法定义放到一个文件中。因为模板类不是函数不能单独编译
template
class myext:public mybase
模板定义两个类型参数T1,T2,定义个非类型参数T3,他在使用期间如同一个常数。这个T3可以用来处理动态数组,并且继承了模板类mybase并且将类型参数T2赋予给他
template
class myext:public mybase
模板定义一个类型参数T2,定义一个非类型参数T3,那么这里的T2就代表他是泛型的,而不具体化,但是开始的T1变为了具体化的int,并且继承了模板类mybase并且将类型参数T2赋予给他
template
void myext::set(const T1 &in)
这是模板类定义方法的方法
template
void myext::set(const int &in)
这是第二个模板类定义方法的方法
调用:
myext test2("test","gaopeng"); 将匹配template class myext:public mybase
myext test1(1,1); 将匹配template class myext:public mybase
具体化过后当然选择的方法是选择匹配程度模板。
下面是具体的测试代码没有实际意义:
点击(此处)折叠或打开头文件
tmp.h
#include
using namespace std;
template
class mybase
{
private:
T1 a;
public:
mybase(T1 in)
{
a=in;
}
virtual void show(void) const
{
cout<
}
virtual ~mybase(){}
};
template
class myext:public mybase
{
private:
T1 b;
public:
myext(T1 in1,T2 in2):mybase(in2)
{
b=in1;
}
virtual void show(void) const
{
for(int i=0;i
cout<
mybase::show();
}
virtual ~myext(){}
virtual void set(const T1&);
};
template
void myext::set(const T1 &in)
{
b=in;
for(int m=0;m
{
cout<<"test"<
}
}
template
class myext:public mybase
{
private:
int b;
public:
myext(int in1,T2 in2):mybase(in2)
{
b=in1+100;
}
virtual void show(void) const
{
for(int i=0;i
cout<
mybase::show();
}
virtual ~myext(){}
virtual void set(const int&);
};
template
void myext::set(const int &in)
{
b=in+100;
for(int m=0;m
{
cout<<"test1"<
}
}
点击(此处)折叠或打开main函数
#include
#include"tmp.h"
using namespace std;
int main(void)
{
cout<<"use specialization template"<
myext test1(1,1);
test1.set(10);
test1.show();
cout<<"use general template"<
myext test2("test","gaopeng");
test2.show();
}
输出:
use specialization template
test1
test1
test1
test1
test1
test1
test1
test1
test1
test1
110
110
110
110
110
110
110
110
110
110
1
use general template
test
test
test
test
gaopeng