Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

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;
};
 
PREVIOUS NEXT
Tagged: #unary #overload
ADD COMMENT
Topic
Name
5+1 =