Overloading unary operators
Unlike the operators you’ve seen so far, the positive (+), negative (-) and logical not (!) operators all are unary operators, which means they only operate on one operand. Because none of these operators change their operands, we will be implementing them as friend functions. All three operands are implemented in an identical manner.
Let’s take a look at how we’d implement operator- on the Cents class we used in a previous example:
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents) { m_nCents = nCents; }
// Overload -cCents
friend Cents operator-(const Cents &cCents);
};
// note: this function is not a member function!
Cents operator-(const Cents &cCents)
{
return Cents(-cCents.m_nCents);
}
This should be extremely straightforward. Our overloaded negative operator (-) takes one parameter of type Cents, and returns a value of type Cents.
Here’s another example. The ! operator is often used as a shorthand method to test if something is set to a non-zero value. For example, the following example would only execute if nX were non-zero:
if (nX)
// do something
Similarly, we can overload the ! operator to work similarly for a user-defined class:
class Point
{
private:
double m_dX, m_dY, m_dZ;
public:
Point(double dX=0.0, double dY=0.0, double dZ=0.0)
{
m_dX = dX;
m_dY = dY;
m_dZ = dZ;
}
// Convert a Point into it's negative equivalent
friend Point operator- (const Point &cPoint);
// Return true if the point is set at the origin
friend bool operator! (const Point &cPoint);
double GetX() { return m_dX; }
double GetY() { return m_dY; }
double GetZ() { return m_dZ; }
};
// Convert a Point into it's negative equivalent
Point operator- (const Point &cPoint)
{
return Point(-cPoint.m_dX, -cPoint.m_dY, -cPoint.m_dZ);
}
// Return true if the point is set at the origin
bool operator! (const Point &cPoint)
{
return (cPoint.m_dX == 0.0 &&
cPoint.m_dY == 0.0 &&
cPoint.m_dZ == 0.0);
}
In this case, if our point has coordinates (0.0, 0.0, 0.0), the logical not operator will return true. Otherwise, it will return false. Thus, we can say:
Point cPoint; // use default contructor to set to (0.0, 0.0, 0.0)
if (!cPoint)
cout << "cPoint was set at the origin." << endl;
else
cout << "cPoint was not set at the origin." << endl;
which produces the result:
cPoint was set at the origin.
9.6 — Overloading operators using member functions
|
Index
|
9.4 — Overloading the comparison operators
|
9.6 — Overloading operators using member functions
Index
9.4 — Overloading the comparison operators
Is there an operator I can use to overload the usual if() effect? E.g.,
if (cPoint) cout < < "cPoint is not at the origin." << endl; else cout << "cPoint is at the origin." << endl;What would happen in this case? Would C++ call the ! operator and reverse the result? Or is there another way to provide a boolean operator?
I think you could do this via overloading typecasts. If you provide an overloaded boolean conversion operator for your point, then I think the above example would work. This is covered in lesson 9.10.
However, personally I wouldn’t recommend implementing it this way. The conversion from point to boolean isn’t very intuitive. I think this is better implemented as a member function that returns a boolean result — in this way, your code will be more self-documenting. eg:
if (cPoint.IsOrigin()) // etc