Please support our C++ advertiser: Programming Forums
![]() |
•
•
Join Date: Jul 2004
Location: North East Indiana
Posts: 491
Reputation:
Rep Power: 5
Solved Threads: 20
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.
I have the class and template set up, as shown here:
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:
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:
safe_array<char, 5> dosomething;
What I want is for this line:
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
Location: Tucson, Az
Posts: 107
Reputation:
Rep Power: 5
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
Other Threads in the C++ Forum
- Stuck with "Safe Mode" display (Windows 9x / 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
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)





Linear Mode