Connecting Tech Pros Worldwide Help | Site Map

Unary + / -

  #1  
Old July 19th, 2005, 06:27 PM
Dave Theese
Guest
 
Posts: n/a

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


  #2  
Old July 19th, 2005, 06:27 PM
Oliver S.
Guest
 
Posts: n/a

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 ()).
  #3  
Old July 19th, 2005, 06:28 PM
Shane Beasley
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is cast operator unary or binary? How many operands? JoseMariaSola answers 16 June 27th, 2008 08:36 PM
Associativity of unary C Operators dspfun answers 28 January 5th, 2007 07:25 AM
Template friend (unary and binary) operators Ruben Campos answers 5 July 22nd, 2005 11:22 PM
Unary plus operator and __pos__ Carlos Ribeiro answers 3 July 18th, 2005 04:57 PM