例:
template< typename T >
class TArray {
protected:
T m_data[];
public:
bool Append(T item) {
int new_size = ArraySize(m_data) + 1;
int reserve = ( new_size / 2 + 15 )&~ 15;
if ( ArrayResize(m_data,new_size , reserve) != new_size) {
return( false );
}
m_data[new_size - 1] = item;
return( true );
}
T operator[](int index) {
static T invalid_index;
if ( index < 0 || index >= ArraySize(m_data) ) {
return( invalid_index );
}
return( m_data[index] );
}
};
template<typename T>
class TArrayPtr : public TArray&:ltT *> {
public:
void ~TArrayPtr() {
for ( int n = 0, count = ArraySize(m_data) ; n < count ; n++ ){
if ( CheckPointer( m_data[n]) == POINTER_DYNAMIC ) {
delete m_data[n];
}
}
}
};
class CFoo {
int m_x;
public:
CFoo(int x):m_x(x) { }
int X(void) const { return(m_x); }
};
TArray<int> ExtIntArray;
TArray<double> ExtDblArray;
TArrayPtr<CFoo> ExtPtrArray;
void OnStart() {
for( int i=0; i < 10 ; i++ ) {
int integer = i + 10;
ExtIntArray.Append( integer );
double dbl = i + 20.0;
ExtDblArray.Append( dbl );
CFoo *ptr = new CFoo( i + 30 );
ExtPtrArray.Append( ptr );
}
string str = "Int:";
for( i = 0 ; i < 10 ; i++ ) {
str += " " + (string)ExtIntArray[i];
}
Print(str);
str = "Dbl:";
for( i=0 ; i < 10 ; i++ ) {
str += " " + DoubleToString(ExtDblArray[i],1);
}
Print(str);
str = "Ptr:";
for( i = 0 ; i < 10 ; i++ ) {
str += " " + (string)ExtPtrArray[i].X();
}
Print(str);
}
実行結果:
TemplTest EURUSD,M1: Ptr: 30 31 32 33 34 35 36 37 38 39
TemplTest EURUSD,M1: Dbl: 20.0 21.0 22.0 23.0 24.0 25.0 26.0 27.0 28.0 29.0
TemplTest EURUSD,M1: Int: 10 11 12 13 14 15 16 17 18 19