Jump to content
  • Advertisement
Sign in to follow this  
random_thinker

Overloading postfix increment/decrement operator, accept type other than int?

This topic is 3927 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

I've been trying to overload postfix increment/decrement operators with a type other than int, but everything I see indicates that this cannot be done. Apparently the form must have an 'int' argument as this is the only thing that the compiler will recognize. Is anyone aware of a way around this? ex:
// Case 1: Standard method...
class myClass
{
myClass operator--(int i)
{
    myClass copy(*this);
    --value_;
    return copy;
}
};

// Case 2: Desired method...
class myClass
{
myClass operator--(myClass& m)
{
    myClass copy(*this);
    --m.value_;
    return copy;
}
};
--random

Share this post


Link to post
Share on other sites
Advertisement
Unary overloaded operators inside a class declaration do not take an explicit argument (as the "this" pointer suffices).

operator--() should work, but will only overload the pre-decrement version.

When faced with the what the post-decrement operators function signature should look like, the C++ standards writers basically cheated. If you declare operator--(int) in your class then that code will be called when you try to call foo--; where foo is an object of MyClass. The int has no meaning (other than to disambiguate which operator you are overloading).


struct Foo {
Foo operator++(int) { Foo temp(*this);/* do work */ return temp; }
};

void Example( Foo foo ) {
foo++;
}




Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Why would you want to?


Hi Si,

I don't know if you saw in another thread, but I was experimenting with a safe integer type for iteration, that could overcome potentially unsafe integer assignments, catch divide by zero buffer overflow conditions, incorrect array indexing, incorrect iteration values, etc. This quickly led to these postfix overload issues, and some other issues such as type conversion behaviour.

At the moment I'm unable to post decrement without manually inserting the code for each occurrence. I suppose that a function of some sort could be used...

--random

Share this post


Link to post
Share on other sites
Quote:
Original post by random_thinker
I've been trying to overload postfix increment/decrement operators with a type other than int, but everything I see indicates that this cannot be done. Apparently the form must have an 'int' argument as this is the only thing that the compiler will recognize. Is anyone aware of a way around this?


There is no way around it. It's a part of the language definition. The actual argument is never used and there's no way you can pass a value to it, it's just an artefact of how you specify postdecrement operators.

--smw

Share this post


Link to post
Share on other sites
Yeah, I think you may be slightly confused as to the purpose of the int. It is never used at all. They may as well have chosen bool or void*. It's only there so that the argument list for postincrement is different from the argument list for preincrement. There's no reason to use anything else.

Share this post


Link to post
Share on other sites
From what I've read, it's a little bit of a compiler hack, in that where it sees the int it inserts a zero and the proceeds with the overload. But I was wondering if the post increment/decrement could take arguments like custom ostream formatter functions do. I have seen that you can also have non-member post increment/decrement operator functions that will take a class as an arguement, something like:


myClass operator++(myClass& mc, int i)
{
// do the post increment thingy...
}


But I was hoping that there might be some obscure work-around.

--random

Share this post


Link to post
Share on other sites
I don't think I understand what you are asking. What would the client code for such an overloaded ++ operator look like?

Give an example of the code you would like to compile, client code and all and then I might have some idea.

Until then...

Share this post


Link to post
Share on other sites
Quote:
Original post by random_thinker
From what I've read, it's a little bit of a compiler hack, in that where it sees the int it inserts a zero and the proceeds with the overload. But I was wondering if the post increment/decrement could take arguments like custom ostream formatter functions do.

For ostream, << is a binary operator. How would ++ take an extra argument? It's a unary operator. Where would you shove the other argument... between the two plus signs?

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!