944,041 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5300
  • C++ RSS
Sep 12th, 2005
0

"Safe array" assignment

Expand Post »
Hello,

I've been trying to write a program for a class I'm taking, and have gotten stuck on part of the assignment.

The program is to:
Quote ...
9> Create a template for a safe array. The user will be able to designate the element type and the size of the array. The operations will ensure that no access is allowed outside the bounds of the array. Operations will include [], =, and ==. Instantiate several arrays of different types and sizes and show that the safety features work.
I have the class and template set up, as shown here:
C++ Syntax (Toggle Plain Text)
  1. template <class datatype, int size>
  2. class safe_array
  3. {
  4. private:
  5. //Note: array format: TYPE Name[element]
  6. datatype thearray[size];
  7. bool checkbounds(int ub); //Return true if good, false if not.
  8.  
  9. public:
  10. safe_array();
  11. ~safe_array();
  12. datatype operator [] (int index);
  13. datatype operator = (datatype value);
  14. datatype operator == (safe_array& array1);
  15.  
  16. //These are temporary. They will be used in testing.
  17. void fill();
  18. void print();
  19. };

The rest of the definition is as follows:
C++ Syntax (Toggle Plain Text)
  1. template <class datatype, int size>
  2. safe_array<datatype, size>::safe_array()
  3. {
  4. }
  5.  
  6. template <class datatype, int size>
  7. safe_array<datatype, size>::~safe_array()
  8. {
  9. }
  10.  
  11. template <class datatype, int size>
  12. bool safe_array<datatype, size>::checkbounds(int ub)
  13. //bool checkbounds(int ub)
  14. {
  15. //ub--upper bound
  16. if (ub <= size)
  17. {
  18. return true;
  19. }
  20. else
  21. {
  22. return false;
  23. }
  24. }
  25.  
  26. template <class datatype, int size>
  27. datatype safe_array<datatype, size>::operator [](int index)
  28. {
  29. bool goodornot;
  30. goodornot = checkbounds(index);
  31. if (goodornot)
  32. return thearray[index];
  33. else
  34. {
  35. cout << "Run-time error: Index out of bounds.";
  36. cerr << "Index out of bounds!!!";
  37. return -1;
  38. }
  39. }
  40.  
  41. template <class datatype, int size>
  42. datatype safe_array<datatype, size>::operator = (datatype value)
  43. {
  44.  
  45. }
  46.  
  47. template <class datatype, int size>
  48. datatype safe_array<datatype, size>::operator == (safe_array& array1)
  49. {
  50. }
  51.  
  52. //Temporary
  53. template <class datatype, int size>
  54. void safe_array<datatype, size>::fill()
  55. {
  56. cin >> thearray[1];
  57. cin >> thearray[2];
  58. cin >> thearray[3];
  59. cin >> thearray[4];
  60. }
  61.  
  62. template <class datatype, int size>
  63. void safe_array<datatype, size>::print()
  64. {
  65. cout << thearray[1] << endl;
  66. cout << thearray[2] << endl;
  67. cout << thearray[3] << endl;
  68. cout << thearray[4] << endl;
  69. }

The template is instantiated with:
C++ Syntax (Toggle Plain Text)
  1. safe_array<char, 5> dosomething;

What I want is for this line:
C++ Syntax (Toggle Plain Text)
  1. dosomething[5] = 'w';

to do what it would normally do using an array. I'm stuck on writing the assignment operator and lengthy web searches have returned no useful results. What do I need to do?
Similar Threads
Reputation Points: 23
Solved Threads: 23
Posting Pro in Training
Puckdropper is offline Offline
494 posts
since Jul 2004
Sep 12th, 2005
0

Re: "Safe array" assignment

You would need to change your [] operator to return a reference and then you could use that as you would.
Reputation Points: 14
Solved Threads: 4
Junior Poster
prog-bman is offline Offline
108 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: STL question concerning partial_sum?
Next Thread in C++ Forum Timeline: a class problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC