[C++] Member function headache

From: Heikki Salo (heikki_salo_at_nobulkmailplease.sci.fi)
Date: 11/21/03


Date: Fri, 21 Nov 2003 11:30:09 +0200

I am designing a geometry class collection (for personal use) and I have
encountered a little problem. It is not technical, but still annoying. I
have an abstract base class wich has some methods that must be overrided in
the derived class. There is one overloaded function to check if two areas
overlap (areas can be rectangles, circles, points...).

class Area {
public:
     bool overlap(Rectangle&) = 0;
     bool overlap(Circle&) = 0;
     ... // more functions for another type of areas
    };

The problem is that when I have a rectangle class which has an overlap
funtion defined for circle, I have to define circle an overlap function for
rectangle. The functions are almost completely identical. One solution is to
make circles overlap funtion like this:

bool Circle::overlap(Rectangle& rect) {
     return rect.overlap(*this);
     }

I believe that virtual functions can not be inlined (?), so the previous
solution wastes some time. I could also put the same code in both functions,
but it just does not feel right. Any good ideas?

I have also another question conserning pass-by-reference, which those
funtions use. C++ FAQLite says that pass-by-value can be faster, which was
quite a surprise for me. Should I use pass by reference or value?



Relevant Pages

  • Re: Curious little math riddle - Gap or Overlap? - 2nd reply
    ... >> It is not a gap; it is an overlap, or a negative gap, if one wants to insist on the word "gap". ... Depart from the 21x8 rectangle instead of from the 13x13 square. ... Cut the 21x8 rectangle into the four pieces determined by the diagonal and the two vertical lines. ...
    (sci.math)
  • Re: [C++] Member function headache
    ... Does this change the Rectangle? ... So why not bool overlap(Rectangle const &) const = 0; ... > The problem is that when I have a rectangle class which has an overlap ... > funtion defined for circle, I have to define circle an overlap function for ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Circle-Circle Intersection
    ... their overlap comprises a desired area. ... then 3 circles. ... If the _latter_ is your objective, why not try instead Karnaugh maps? ... Because Karnaugh maps are equivalent to Venn diagrams. ...
    (sci.math)
  • Re: Ordering autoshapes
    ... the middle rectangle covers the left end of the right-most rectangle. ... shapes needs to be both on top and bottom at the same time... ... "Send backward" commands moved the object up or down the stack, ... and the bottom one _cannot_ overlap the top one. ...
    (microsoft.public.word.drawing.graphics)
  • Re: Proportional Venn Diagrams
    ... circles' areas and the overlapping portion to be proportionate to their ... Since you say B and C are entirely inside A, your problem boils down to getting just two circles to overlap. ... You can get the proportionality of the circles easily enough using a Bubble Chart type, but Excel bubble charts don't preserve the relationship between bubble radius and XY scale properly, so trigonometry wouldn't work there. ...
    (microsoft.public.excel.charting)