I have a template class called Array that comprises a type and a nontype parameter.
I have the following code for my constructor.
1 2 3 4 5 6 7 8 9 10 11
template <typename T, int arraySize>
Array<T,arraySize>::Array()
: size( arraySize )
{
ptr[arraySize]; // create space for pointer-based array
cout << ptr[0] << endl;
}
I have been told not to use the new operator because it is unnecessary because the elements will be initialized by the default constructor of whatever class they belong to. However, every time I try and access the first element of the array to print it out or change it I get an unhandled exception.
template <class T, int size> class Array
{
friend ostream &operator<<( ostream &, const Array & );
friend istream &operator>>( istream &, Array & );
public:
Array(); // default constructor
int getSize() const; // return size
staticint getArrayCount();
void inputArray();
const T &operator=( const Array & ); // assignment operator
booloperator==( const Array & ) const; // equality operator
// inequality operator; returns opposite of == operator
booloperator!=( const Array &right ) const
{
return ! ( *this == right ); // invokes Array::operator==
} // end function operator!=
// subscript operator for non-const objects returns modifiable lvalue
T &operator[]( int );
// subscript operator for const objects returns rvalue
T operator[]( int ) const;
private:
int size; // pointer-based array size
int *ptr; // pointer to first element of pointer-based array
staticint arrayCount;
}; // end class Array
Here's my constructor for the template class.
1 2 3 4 5 6 7 8 9 10 11 12
// default constructor for class Array (default size 10)
template <typename T, int arraySize>
Array<T,arraySize>::Array()
: size( arraySize )
{
ptr[arraySize]; // create space for pointer-based array
cout << ptr[0] << endl;
arrayCount++;
} // end Array default constructor
Here's the code I was given for the constructor when it was a non-template class.
1 2 3 4 5 6 7 8 9
// default constructor for class Array (default size 10)
Array::Array( int arraySize )
{
size = ( arraySize > 0 ? arraySize : 10 ); // validate arraySize
ptr = newint[ size ]; // create space for pointer-based array
for ( int i = 0; i < size; i++ )
ptr[ i ] = 0; // set pointer-based array element
} // end Array default constructor
How do I make the constructor for my template class perform the same job as the previous constructor without the new operator and to make sure it is initialised appropriately for what the T type is?
You want a pointer based array.
Pointer based array points to something.
That something can be allocated either dynamically or statically.
If you allocate a static array in the constructor and set a pointer to it, when constructor ends, the memory will be deallocated, so you can't do that.
You could allocate the static array in the scope of the function which uses your class, but that would defeat the purpose of having that class at all.
You don't want to use new, so dynamic allocation is impossible.
Thus, clearly, a pointer based array is not what you need.
The only other option is what I wrote in my previous post.