12.
.
.
, , C++ , .
C++ .
, .
(template instantiation).
, , .
.
, - .
, , .
( ) . , , .
, , , , .
, , .
, , , .
:
, class typename, :
template < typename >
class point
{ /*... */ };
, .
, int.
, , .
, . , , n :
template <class , int n>
class Array
{ / *... * / };
, :
Array<point, 20> ;
20 point.
:
void f1()
{ cout << "I am f1 (). " << endl; }
|
|
void f2()
{ cout << "I am f2 (). " << endl; }
template<void (*pf)()>
struct A
{ void Show()
{ pf();}
};
int main()
{
A<&fl> aa;
aa. Show(); // : I am f1().
A<&f2> ab;
ab. Show();// : I am f2().
return 0;
}
. . , .
:
template<class T=char, int size=64>
class arr {
T data [size];
int length;
public:
arr():length(size){ }
T& operator [ ] (int i) {
if(i<0 | | i> size){ cout<<Index error; exit(1);}
return data[i];
}
};
int main () {
arr<double,5> rf;
arr<int>ri; // int, size
arr< > rc; //
for(int i=0; i<5; i++)
{ rf[i]=i; ri=i*i; rc=A+i; }
for(int i=0; i<5; i++)
cout<<rf[i]<< << ri[i]<< <<rc[i]<< \t;
return 0;
}