Connecting Tech Pros Worldwide Help | Site Map

Unary + / -

Dave Theese
Guest
 
Posts: n/a
#1: Jul 19 '05

Am I correct in saying that it is not possible to overload unary + and
unary -?


Oliver S.
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Unary + / -


> Am I correct in saying that it is not possible to overload[color=blue]
> unary + and unary -?[/color]

You're not. The following unary operators are overloadable:
! Logical NOT
& Address-of
~ One's complement
* Pointer dereference
+ Unary plus
++ Increment
– Unary negation
–– Decrement
And all of them are overloadable through a global function as
well as a non-static member-function (btw: the only operators
that are only overloadable as non-static members are =, ->, []
and ()).
Shane Beasley
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Unary + / -


"Dave Theese" <cheeser_1998@yahoo.com> wrote in message news:<iTv8b.1611$v%5.257@fed1read02>...
[color=blue]
> Am I correct in saying that it is not possible to overload unary + and
> unary -?[/color]

No.

class S {
public:
S (int value = 0) : myValue(value) { }
S operator+ () const { return -myValue; }
S operator- () const { return myValue; }

private:
int myValue;
};

int main () {
S x;
x = +x;
x = -x;
}

- Shane
Closed Thread