Overloading the ++ for a node

Started by
9 comments, last by NotAYakk 17 years, 11 months ago
Say I have a node for a link list (someone please tell me how to tab) struct node { int data; node* next; } How can I overload the ++ operator so I can use my node like for(node* iter = head; iter != 0; ++iter) { //do something with link }
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Advertisement
Quote:Original post by simon10k
Say I have a node for a link list (someone please tell me how to tab)

struct node {
int data;
node* next;
}

How can I overload the ++ operator so I can use my node like

for(node* iter = head; iter != 0; ++iter)
{
//do something with link
}


Forum Tags

You don't overload ++ on the node itself (I suppose you could if you really wanted, but that's some bizarre semantics). See Iterator Pattern

It would probably look something like
struct node_iterator {  node* n;  void operator ++ (int) {    n = n->next;  }};


Of course there's a slew of other operators you would probably want to overload as well as not having operator ++ returning void, this is just as simple as I could make it. There are even more considerations to make if you want your data type to work with STL containers.
[size=2]
You can overload operator++ on the node, but your probably shouldn't - you'll see why below.

Anyway:
struct node{   // Post-increment, e.g. it++   node operator++()   {      node old(this);      next=next->next;      return old;   }   // Pre-increment, e.g. ++it   node& operator++(int)   {      next=next->next;      return *this;   }int data;node* next;}
The reason you'd want an iterator is that it doesn't really make sense to increment a node itself here. You'd almost certainly want an iterator to do that instead.
Also note that he's using ++ on a pointer there, which you can't overload at all [attention]
Basically I'd rather have my code look like...

++iter;

than...

iter = iter->next;
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Quote:Original post by simon10k
Basically I'd rather have my code look like...

++iter;

than...

iter = iter->next;


Don't confuse the object itself with the iterator referring to that object. If it helps, you can think of an iterator as a nicely wrapped pointer. See the link I posted or just google for iterator pattern to find loads more info.

Also, aside from pretty bizarre semantics of having operator ++ on a node, the syntax you want is impossible. The best you could do is (*iter) ++.
[size=2]
I think I understand now.
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Quote:Original post by simon10k
How can I overload the ++ operator


You're clearly using C++ (see quote) and clearly don't know quite what you're doing (see other responses); you should therefore be using some standard library container instead of trying to reinvent the wheel. std::list is a linked list. Other resizable containers are provided (depending on how you were educated, you might not have ever been asked to consider other ways of implementing resizable storage) such as std::vector and std::deque (sequential) and std::map (associative).
Quote:Original post by Zahlman
Quote:Original post by simon10k
How can I overload the ++ operator


You're clearly using C++ (see quote) and clearly don't know quite what you're doing (see other responses); you should therefore be using some standard library container instead of trying to reinvent the wheel. std::list is a linked list. Other resizable containers are provided (depending on how you were educated, you might not have ever been asked to consider other ways of implementing resizable storage) such as std::vector and std::deque (sequential) and std::map (associative).


I didn't ask how to overload the ++ operator you fool. You cut my sentance in half. The purpose of this little experiment was for educational purposes. You wouldn't make a good mechanic if you only knew how to drive a car. I am not an expert, and I thank you for pointing that out, but neither are you.

-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
No need to be snippy; Zahlman's answer is completely correct.

What you want to do involves overloading the ++ operator for a specific type (your node struct). So the quotation in question is not at all inaccurate. Did you perhaps think he was implying you wanted to overload ++ for all types? (Which, by the way, is not really how it works - you overload operators for only one type at a time in C++.)

That aside, Zahlman is absolutely correct in stating that you should favor using STL containers instead of building your own. And for the record, he's one of the resident experts on this site.

However, it is of course reasonable to build your own containers once or twice just to understand better how they work. If that is what you're doing (sounds likely) then it's fine, but you haven't said so explicitly, so as far as any of us know you're doing this for "real" code and not just as a mental exercise.


Miscommunication happens, and happens a lot on the internet especially, because all we have is text and no body language or intonation to help clarify our meanings. Just keep that in mind before assuming the worst of someone and pulling out the bag of insults [wink]

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement