c++ 模板类

C++模板类,相当于Java中的泛型,不过模板类的范围更加广阔,可以使用基本数据类型;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
template<class T>
class A{
public:
A(T t){
this->t = t;
}
protected:
T t;
};

// 继承模板类
class B :public A<int>{
public:
B(int a) :A<int>(a){

}
};

template<class E>
class C :A<int>{
public:
C(E e, int a) :A(a){
this->e = e;
}

protected:
E e;
};

template<class T>
class D :A<T>{
public:
D() :A(10){}

D(T t) :A(t){
}
};

void main(){

A<char*> str("hihi");

B b(10);
C<int> c(10,10);

D<int> d1;

D<int> d2(10);

system("pause");
}