Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

operator overloading

Status
Not open for further replies.

sdmoochew2

Programmer
May 10, 2002
3
AU
Hi there!
Ok. I am writing a simulated electronic diary. It takes a user input time, date and event/appiontment.
I have created a stack and each input is added as a node.
But, I am trying to write and option that displays the node #n, where n is the user input. SO im trying to make my stack like an array and display eg. MyStack[2].display()
However im not sure how to overload the [] operator.
CAN ANYONE PLEASE HELP?? It would be greatly appreciated.
 
Do you mean just the syntax for overloading it or the actual implementation?

If you just want the syntax, you'd make a member function to the effect of
Code:
T &operator[]( unsigned );
(and
Code:
const T &operator[]( unsigned ) const;
) where T is the type of whatever item you're trying to get.

For the implementation, it depends on how you have your stack is set up. If it's an array, you'll just have the operator return the item that's in the given index. If it's a linked list, you'll have to step through the nodes until you hit the specified one, then return the data.
 
thanx you.
Yes it is a linked list. And yes I want to return the data a node #n.

And now i have it working
Thanx a lot for ur help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top