Answers for "c++ unary minus overload"

C++
1

c++ unary minus overload

class A{
public:
	A operator-() const{
    	A temp;
    	temp.someData = -std::abs(temp.someData);
      	return temp;
    } // Or you can make a friend operator
  	friend A operator-(A un){ // You can use a reference instead but then
    	un.someData = -std::abs(un.someData);     // you have to create a
      	return un;                                // temporary object.
    }                                             // Not a big difference
private:
	int someData;
};
Posted by: Guest on April-05-2022

Browse Popular Code Answers by Language