How to Overload Unary Minus Operator in C++

How to overload unary minus operator in C++?

Yes, but you don't provide it with a parameter:

class Vector {
...
Vector operator-() {
// your code here
}
};

Note that you should not return *this. The unary - operator needs to create a brand new Vector value, not change the thing it is applied to, so your code may want to look something like this:

class Vector {
...
Vector operator-() const {
Vector v;
v.x = -x;
v.y = -y;
return v;
}
};

C++ is it possible to overload the unary minus operator of an rvalue reference?

You can use reference qualifier-s (or ref-qualifiers as in standard)

http://coliru.stacked-crooked.com/a/40905649dc0c14e7

example:

#include <iostream>
#include <string>
#include <vector>

class X
{
public:
int n;
X(const X&) = default;
X() : n(0) {}
X(int n) : n(n) {}

X operator- () const & // lvalue ref-qualifier
{
std::cout << "&\n";
X x(-n);
return x;
}

X operator- () const && // rvalue ref-qualifier
{
std::cout << "&&\n";
X x(-n);
return x;
}

friend X operator+(const X& lhs, const X& rhs) {
return X(lhs.n + rhs.n);
}
};

int main()
{
X a;
X b = -a; // unary operator- of a TYPE& aka lvalue ref
X c = -(a+b);
}

outputs:

&
&&

- minus unary operator overloading in c++

if I run program without making it comment the program also works fine than in what purpose is this statement is using

From [stmt.return]:

Flowing off the end of a constructor, a destructor, or a function with a cv void return type is equivalent to a return with no operand. Otherwise, flowing off the end of a function other than main ([basic.start.main]) results in undefined behavior.

Your program is resulting an undefined behavior, anything can happen.

Second, is it constructor function returning values?

I think you meant this line:

return Distance(feet, inches);

That line return a prvalue of Distance which is constructed from feet and inches. That value should be eligible to copy-elision and is guaranteed to be elided in copy from C++17

Third, how it is returning values I mean it is not a variable I always heard we can return values from variable?

From the said [stmt.return], emphasis is mine:

The expr-or-braced-init-list of a return statement is called its operand. A return statement with no operand shall be used only in a function whose return type is cv void, a constructor ([class.ctor]), or a destructor ([class.dtor]). A return statement with an operand of type void shall be used only in a function whose return type is cv void. A return statement with any other operand shall be used only in a function whose return type is not cv void; the return statement initializes the glvalue result or prvalue result object of the (explicit or implicit) function call by copy-initialization ([dcl.init]) from the operand. [ Note: A return statement can involve an invocation of a constructor to perform a copy or move of the operand if it is not a prvalue or if its type differs from the return type of the function. A copy operation associated with a return statement may be elided or converted to a move operation if an automatic storage duration variable is returned ([class.copy]). — end note ] [ Example:

 std::pair<std::string,int> f(const char* p, int x) {
return {p,x};
}

The return statement should go in those form:

return; // for void and constructor, destructor

or

return expression-or-braced-init-list;

Anyway, I think your minus operator should look like this:

 Distance operator- () {
return Distance(-feet, -inches);
}

How to overload both unary and binary minus operators in C++?

1.v is passed by reference to const, it can't be called with non-const member function. Since operator- and operator+ (both unary and binary version) doesn't modify members of class, you should make them const member functions.

Vector Vector::operator-() const {
return Vector(-x,-y,-z);
}

2.Change

return this* + (-v);

to

return *this + (-v);

how to overload minus operator to subtract two fractional numbers?

Use this GCD (greatest common divisor) function to implement your HCF function. Yours currently exhibits an endless loop when given 0 as a first argument.

To reduce a fraction, divide numerator and denominator by their greatest common divisor.

This is called the Euclidean algorithm.

template <typename T>
T GCD(T a, T b) {
while (b != 0)
{
T t = a % b;
a = b;
b = t;
}
return std::max(a, -a);
}

Difficulty in unary operator overloading in C++?

The unary minus operator - is a prefix operator only.

to overload suffix version of the ++ operator, you need a dummy int parameter. e.g.

struct foo
{
void operator - ()
{
std::cout << "hello" << std::endl;
}

void operator ++ (int)
{
std::cout << "world" << std::endl;
}
};

int main()
{
foo bar;
-bar;
bar++;
}

how to apply operator overloading for unary postfix operator

if you want post-inc/dec then the code will be :

  Distance operator++ (int) {
feet = feet+1;
inches = inches+1;

return Distance(feet, inches);
}

we use int in formal parameter . it is just crate different between post/pre-fix.
The prefix form of the operator is declared exactly the same way as any other unary operator; the postfix form accepts an extra argument of type int.



Related Topics



Leave a reply



Submit