I've a template where I want to use a pointer type or an instance type as the template parameter. An Example:
class FOO {};
class ItemGetter {
virtual FOO * GetItem(int index) = 0;
};
template<class T>
class ArrayContainer : public ItemGetter {
T *array;
ArrayContainer(T * array) {
this->array = array;
}
/*
* A lot of other functions.
* I want to write them only once
* ....
*/
/* If the T Parameter is an instance Type */
FOO * GetItem(int index) {
return &this->array[index];
}
/* If the T Parameter is an pointer Type */
FOO * GetItem(int index) {
return this->array[index];
}
};
void usage() {
FOO fooArray[100];
FOO * fooPointerArray[100];
auto bar1 = ArrayContainer<FOO>(fooArray);
auto bar2 = ArrayContainer<FOO*>(fooPointerArray);
}
How can I specialize the GetItem() function for the pointer and the instance variant (without writing all the other functions twice)?
Aucun commentaire:
Enregistrer un commentaire