Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <stdio.h>
- #include <new>
- #ifdef _MSC_VER
- #define _CRT_SECURE_NO_WARNINGS
- #endif
- template <class aType>
- class SafeArray
- {
- public:
- SafeArray();
- ~SafeArray();
- SafeArray(const aType& index, const aType& pastIndex);
- aType &operator[](const aType& a);
- void setZero();
- private:
- int size;
- int start;
- int end;
- aType *location = NULL;
- };
- template <class aType>
- SafeArray<aType>::SafeArray(const aType& index, const aType& pastIndex)
- {
- size = (pastIndex - index);
- location = new aType[size];
- setZero();
- start = index;
- end = pastIndex;
- }
- template <class aType>
- SafeArray<aType>::~SafeArray()
- {
- delete[] location;
- }
- template <class aType>
- aType &SafeArray<aType>::operator[](const aType& i)
- {
- if (i < start || i >= end)
- {
- printf("Error: This location is out of bounds\n");
- getchar();
- }
- return location[i];
- }
- template <class aType>
- void SafeArray<aType>::setZero()
- {
- for (int i = 0; i < size; i++)
- {
- location[i] = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement