Skip to main content
gamedev.net
🔒 Locked

++ operator overloading

Started by Vetinari Apr 6, 2000 at 5:16 PM 11 replies 1.3k views
Original Post
Vetinari
Vetinari
I would like to overload the ++ operator. I did it for pre-fix (i.e. ++myclass) but I don''t understand how to overload the postfix++ it (i.e. myclass++). Thanks for your help, Mike
"Unintentional death of one civilian by the US is a tragedy; intentional slaughter of a million by Saddam - a statistic." - Unknown
ga
ga
The ++ pre-fix and post-fix operators are the same, the only difference is that with the postfix first the current command is exexcuted and then the ++ operator, with the prefix it''s the other way round. There shouldn''t be a difference with overloaded operators.

Visit our homepage: www.rarebyte.de.st

GA
Visit our homepage: www.rarebyte.de.stGA
3dModelMan
3dModelMan
Sorry ga, there is a difference...

The prefix version:

aclassfunction operator++ ()
{
}

The post-fix version:

aclassfunction operator++(int)
{
}

The ''int'' is just a dummy argument, thats it. Very subtle but very important!

Cheers

Matt

Check out my project at: www.btinternet.com/~Matthew.Bennett
Erik Post
Erik Post
3dModelMan is correct: the postfix operator++ needs a dummy argument of int. The prefix operator++ doesn''t need it.

Make sure that you''re returing the correct value: the postfix operator++ should return the value before the increment, the prefix operator++ should return the value after the increment. Of course you''re allowed to use different semantics, but it''ll confuse the users of your code.

Erik
Vetinari
Vetinari
Yeah, I knew it needed something. Thanks for your help.

Mike
"Unintentional death of one civilian by the US is a tragedy; intentional slaughter of a million by Saddam - a statistic." - Unknown
Kaellaar
Kaellaar
Anonymous,

++ is a unary operator, that function won''t work.
Kylotan
Kylotan
quote:
Original post by Kaellaar

Anonymous,

++ is a unary operator, that function won''t work.


Is it not possible to declare it that way if you made it a friend function of the class rather than a member?
ga
ga
I''m sorry for my wrong answer. I never needed overloading the ++ operator so I really didn''t expect that there would be a difference to the usual ++ operator.

Visit our homepage: www.rarebyte.de.st

GA
Visit our homepage: www.rarebyte.de.stGA
Kaellaar
Kaellaar
I don''t think so, I may be wrong, but I dont'' even think the compiler will let you put anything but an int as a parameter.
null_pointer
null_pointer
If you don''t overload both, the compiler just uses the overloaded one for both cases.


class USDollar
{
public:
USDollar() : nDollars(0), dCents(0) {};

protected:
unsigned int nDollars;
double dCents;

public:
USDollar& operator++();
};

USDollar& USDollar::operator++()
{
dCents++;
if( dCents >= 100 )
{
dCents = 0;
nDollars++;
}
return( *this );
}


main()
{
USDollar MyWallet();

// both of these work fine (in simple expressions)
MyWallet++;
++MyWallet;

return 0; // is this necessary? been a while since console apps...
}






- null_pointer
Sabre Multimedia
Mezz
Mezz
people who said you need a dummy argument are correct.

e.g.

class poo
{
int x;
poo operator++();
poo operator++(int notused);
};

// then just define the member functions

I looked that up in TYC++ third edition
just before making up that crappy example
the compiler distinguishes between the
pre and post-fix operators by that argument,
and in any calling case, it is passed the value of
zero.

Take it easy,

-Mezz
Vetinari
Vetinari
Null, I know that it works even if you don''t overload both, but I wanted them to be slightly different.

Mike
"Unintentional death of one civilian by the US is a tragedy; intentional slaughter of a million by Saddam - a statistic." - Unknown

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.