Advertisement

++ operator overloading

Started by April 06, 2000 05:16 PM
11 comments, last by Vetinari 24 years, 8 months ago
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
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
Advertisement
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

This topic is closed to new replies.

Advertisement