| | |
"Safe array" assignment
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2004
Posts: 494
Reputation:
Solved Threads: 21
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:
I have the class and template set up, as shown here:
The rest of the definition is as follows:
The template is instantiated with:
What I want is for this line:
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?
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.
C++ Syntax (Toggle Plain Text)
template <class datatype, int size> class safe_array { private: //Note: array format: TYPE Name[element] datatype thearray[size]; bool checkbounds(int ub); //Return true if good, false if not. public: safe_array(); ~safe_array(); datatype operator [] (int index); datatype operator = (datatype value); datatype operator == (safe_array& array1); //These are temporary. They will be used in testing. void fill(); void print(); };
The rest of the definition is as follows:
C++ Syntax (Toggle Plain Text)
template <class datatype, int size> safe_array<datatype, size>::safe_array() { } template <class datatype, int size> safe_array<datatype, size>::~safe_array() { } template <class datatype, int size> bool safe_array<datatype, size>::checkbounds(int ub) //bool checkbounds(int ub) { //ub--upper bound if (ub <= size) { return true; } else { return false; } } template <class datatype, int size> datatype safe_array<datatype, size>::operator [](int index) { bool goodornot; goodornot = checkbounds(index); if (goodornot) return thearray[index]; else { cout << "Run-time error: Index out of bounds."; cerr << "Index out of bounds!!!"; return -1; } } template <class datatype, int size> datatype safe_array<datatype, size>::operator = (datatype value) { } template <class datatype, int size> datatype safe_array<datatype, size>::operator == (safe_array& array1) { } //Temporary template <class datatype, int size> void safe_array<datatype, size>::fill() { cin >> thearray[1]; cin >> thearray[2]; cin >> thearray[3]; cin >> thearray[4]; } template <class datatype, int size> void safe_array<datatype, size>::print() { cout << thearray[1] << endl; cout << thearray[2] << endl; cout << thearray[3] << endl; cout << thearray[4] << endl; }
The template is instantiated with:
C++ Syntax (Toggle Plain Text)
safe_array<char, 5> dosomething;
What I want is for this line:
C++ Syntax (Toggle Plain Text)
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.
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.
•
•
Join Date: Nov 2004
Posts: 108
Reputation:
Solved Threads: 3
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.
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
![]() |
Similar Threads
- Stuck with "Safe Mode" display (Windows 95 / 98 / Me)
- Fresh "Safe Mode" HijackThis Log (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: STL question concerning partial_sum?
- Next Thread: a class problem
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





