Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #pragma once
  2. #include <stdio.h>
  3. #include <new>
  4. #ifdef _MSC_VER
  5. #define _CRT_SECURE_NO_WARNINGS
  6. #endif
  7.  
  8. template <class aType>
  9. class SafeArray
  10. {
  11. public:
  12. SafeArray();
  13. ~SafeArray();
  14. SafeArray(const aType& index, const aType& pastIndex);
  15. aType &operator[](const aType& a);
  16. void setZero();
  17.  
  18. private:
  19. int size;
  20. int start;
  21. int end;
  22. aType *location = NULL;
  23. };
  24.  
  25.  
  26. template <class aType>
  27. SafeArray<aType>::SafeArray(const aType& index, const aType& pastIndex)
  28. {
  29. size = (pastIndex - index);
  30.  
  31. location = new aType[size];
  32. setZero();
  33.  
  34. start = index;
  35. end = pastIndex;
  36. }
  37. template <class aType>
  38. SafeArray<aType>::~SafeArray()
  39. {
  40. delete[] location;
  41. }
  42.  
  43. template <class aType>
  44. aType &SafeArray<aType>::operator[](const aType& i)
  45. {
  46. if (i < start || i >= end)
  47. {
  48. printf("Error: This location is out of bounds\n");
  49. getchar();
  50. }
  51.  
  52. return location[i];
  53. }
  54.  
  55. template <class aType>
  56. void SafeArray<aType>::setZero()
  57. {
  58. for (int i = 0; i < size; i++)
  59. {
  60. location[i] = 0;
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement