"Safe array" assignment

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2004
Posts: 494
Reputation: Puckdropper is an unknown quantity at this point 
Solved Threads: 21
Puckdropper Puckdropper is offline Offline
Posting Pro in Training

"Safe array" assignment

 
0
  #1
Sep 12th, 2005
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:
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:
  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:
  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:
  1. safe_array<char, 5> dosomething;

What I want is for this line:
  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?
www.uncreativelabs.net

Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: "Safe array" assignment

 
0
  #2
Sep 12th, 2005
You would need to change your [] operator to return a reference and then you could use that as you would.
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++

Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC