Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overloading "++" operator 1

Status
Not open for further replies.

Kavius

Programmer
Apr 11, 2002
322
CA
I have been working on a date struct and have all the operators overloaded apropriately, but couldn't figure out how to overload the ++ operator. While I don't think it is actually a very usefull one for what I am doing, I am suddenly curious.
[tt]
public static BigDate operator ++()
{
return(internalvalue++);
}
[/tt]
This errors on compile:
[tt]
Overloaded unary operator '++' only takes one parameter
[/tt]
Alright, so I have to add a parameter. But is the parameter supposed to be? What is its value when ++ is called?

(I'm obviously having the same problem with --, and it seems very ignored in all the samples I could find through Google)
 
I would guess the parameter is the object of type BigDate to perform the operation on. Therefore your operator should be
Code:
public static BigDate operator(BigDate date)
{
    return internalvalue++;
}
In this way you can use the existing value to calculate the return value from the ++ operator. Certainly other unary operators are overloaded in this manner.

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Thanks. That's what I thought, but it seemed odd that you would need to pass anything at all in since the operator is only acting on "this" object.

Thanks for confirming.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top