Unary + / -

D

Dave Theese

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

Oliver S.

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

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 ()).
 
S

Shane Beasley

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top