Jump to content
  • Advertisement
Sign in to follow this  
simon10k

Overloading the ++ for a node

This topic is 4580 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 }

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Also note that he's using ++ on a pointer there, which you can't overload at all [attention]

Share this post


Link to post
Share on other sites
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) ++.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!