Originally posted by kkurz
Hey I am having some trouble getting operator/ to overload to a midpoint formula for a Point class. And yes, I have to make operator/ find the midpoint it was part of the assignment.

Anyway I came up with this:
Code:
// Overloaded operator/ to return the midpoint
// between two Point objects, however it does not
// work and only returns garbage.

Point & Point::operator/(const Point & p) const
{
    return Point(((x + p.x) / 2.0), ((y + p.y) / 2.0));
}
You are returning a reference to a new Point object created on the stack inside the operator/ function. When the function ends, all objects created inside it are destroyed, including the one that you returned a reference to. The reference you returned is then pointing at diddly poo. In this case I'd say return a Point and not a Point reference.