问题
I am facing a problem with the code below which is run on Visual Studio 2008. How do I write the function definition for operator +
when you have a statement to be overloaded as follows?
class Distance
{
private:
int feet,inches;
};
main......
Distance Obj, Obj1(2, 2);
Obj = 3 + Obj1; // This line here
Obj1+3
is easy, but how does this one compiler know that it has to do overloading?
Suppose I have to add the value 3 to the data member feet
of Obj1
.
回答1:
Generally an operator of that form would be declared as:
Distance operator+(int lhs, const Distance& rhs) {
// Assuming the int value represents feet
return Distance(rhs.feet + lhs, rhs.inches);
}
You'd probably also want to define the symmetric:
Distance operator+(const Distance& lhs, int rhs) {
// Assuming the int value represents feet
return Distance(lhs.feet + rhs, lhs.inches);
}
Also, I suggest you heed the advice of James McNellis--your job will be simplified if you just have one member representing lengths in a single unit.
回答2:
Several answers now suggest using a non-member operator+
overload to allow "addition" of Distance
and int
objects. This doesn't really make sense: what does it mean to add a Distance
, which has a unit, to an int
, which does not?
However, it does make sense to add two Distance
objects together. If you have one distance of two feet and add another distance of three feet to it, you get a distance of five feet. This makes sense.
You can accomplish this by overloading operator+
between two Distance
objects (for simplicity, I've assumed that your Distance
only has a single field containing inches. In a real-world application, you wouldn't want to have separate fields for inches and feet. You'd probably want to use an SI unit, like meters, but that depends on the application and is entirely up to you):
Distance operator+(const Distance& lhs, const Distance& rhs)
{
return Distance(lhs.inches + rhs.inches);
}
This doesn't help you, though, if you want to be able to do something along the lines of
Distance d;
d = d + 42; // assume 42 has the same units as a Distance object has
In order to get this to make sense, you can use a converting constructor:
struct Distance
{
Distance(int in = 0) : inches(in) { }
private:
int inches;
};
The constructor here is a converting constructor because it is not explicit
and can be called with a single argument. It allows a single int
(or a value that is implicitly convertible to an int
) to be converted to a Distance
object. This allows you to write
Distance d;
d = d + 42;
Why is this different from using an operator+
overload that takes a Distance
and an int
argument? Simple: it forces the conversion from int
to Distance
to take place before the addition, so the actual addition doesn't have to care about its operands: it simply adds two distances together and lets the Distance
constructors deal with any conversions that need to take place.
回答3:
You need to write a free function (outside the class
):
Distance operator+(int lhs, const Distance& rhs)
{
return ...;
}
If ...
needs to use private members of Distance
then make it a friend in Distance
.
class Distance
{
...
friend Distance operator+(int lhs, const Distance& rhs);
};
Finally, if you want to be able to +
with Distance
on the left-hand side, just define another operator+
overload as above, but with Distance
as the first argument.
All this said, I would avoid doing what you appear to be doing. As a user of your Distance
class, I don't know whether what you are doing would add 3 feet, 3 inches, or 3 meters. If you aren't going to use SI units (meters) then you shouldn't allow addition with non-Distance
values.
回答4:
Apart from overloading the +
operator taking two Distance
objects you should avoid overloading operator+(const Distance &, int)
as pointed out in other answers. It's better to define some constants like:
const Distance feet(1,0);
const Distance inch(0,1);
and also overload the operator*(int, const Distance &)
so that you then can write:
Distance dist = 3 * feet + 5 * inch;
Distance dist2 = dist + 2 * feet;
which is more readable.
来源:https://stackoverflow.com/questions/3633549/operator-overloading-operator-in-c