AndreeaN
asked on
C++ Class Templates with Non-Type Parameters
Hi All,
I would like some help with a class template specialization that includes a non-type parameter. I have managed to do a generic class template for the Array class, which include a non-type parameter N - I have included 3 of the methods below.
The problem I am having is how to specialize for floats. I have included my attempt below, but it's well off the mark as I am getting many compilation errors. I am just not sure of the syntax for specializing class templates and the non-type parameter confuses me further. I though I should rewrite the class definition and replace the T's with floats, but I'm not certain about that anymore either. Can anyone assist?
Many thanks
I would like some help with a class template specialization that includes a non-type parameter. I have managed to do a generic class template for the Array class, which include a non-type parameter N - I have included 3 of the methods below.
The problem I am having is how to specialize for floats. I have included my attempt below, but it's well off the mark as I am getting many compilation errors. I am just not sure of the syntax for specializing class templates and the non-type parameter confuses me further. I though I should rewrite the class definition and replace the T's with floats, but I'm not certain about that anymore either. Can anyone assist?
Many thanks
***HEADER****
template<typename T = int, int N = 10>
class Array {
public:
Array(); // constructor
~Array(); // destructor
int getSize() const; // returns the number of elements in the array
bool operator==(const Array<T, N>&) const; // overloaded == operator
bool operator!=(const Array<T, N>&) const; // overloaded != operator
T & operator[](int); // overloaded [] operator
void inputArray(); // eek. not sure what this should do
void outputArray() const; // outputs the array to the console
static int getArrayCount(); // static method to access the arrayCount
private:
T * ptr; // parametized pointer to an array
int size; // private member holding the number of the elements in the array
static int arrayCount; // keeps track of how many array
};
************ GENERIC IMPLEMENTATION ***********
// initializing static member in file scope
template<typename T, int N>
int Array<T, N>::arrayCount = 0;
// constructor
template<typename T, int N>
Array<T, N>::Array() {
ptr = new T[N];
size = N;
for(int i = 0; i < size; i++)
ptr[i] = 0;
arrayCount++;
}
// destructor
template<typename T, int N>
Array<T, N>::~Array() {
delete[] ptr;
}
********************************************************************************
************ TEMPLATE SPECIALIZATION FOR FLOAT *****************
template<>
int Array<float>::arrayCount = 0;
// constructor
template<int N>
Array<float>::Array() {
ptr = new float[N];
size = N;
for(int i = 0; i < size; i++)
ptr[i] = 0;
arrayCount++;
}
// destructor
template<>
Array<float>::~Array() {
delete[] ptr;
}
***************************************************************************
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.