title: C++ Calling a Double dispatch method with objects that are not relative to each other
body: <p>I have a series of shape objects that can check if they intersect one another (an intersection is true if any part of either object overlaps the other). This works great with double dispatch because the intersection of two objects can be represented relative to either object and still be valid and true.</p>
  
  <p>I recently added (read as: "attempted to add") a Contains method that tests whether or not a shape completely encloses another. The double dispatch fails because two shapes of differing sizes will work for Intersect but not Contains: <code>shape_one</code> is a big circle, and <code>shape_two</code> is a smaller circle completely enclosed by <code>shape_one</code>. The call <code>shape_one.Contains(shape_two)</code> will return true but <code>shape_two.Contains(shape_one)</code> will return false.</p>
  
  <p><strong>Is there a way to implement double dispatch for this type of scenario?</strong></p>
  
  <p>(Title edits are encouraged to increase comprehension of question...)</p>
  
  <p>A sample of the Intersects methods with double dispatch:</p>
  
  <pre><code>bool Rectangle::Intersects(const Shape&amp; shape) const {
      return shape.Intersects(*this);
  }
  
  bool Rectangle::Intersects(const Point&amp; point) const {
      return point.Intersects(*this);
  }
  
  bool Rectangle::Intersects(const Line&amp; line) const {
      return line.Intersects(*this);
  }
  
  bool Rectangle::Intersects(const Rectangle&amp; rectangle) const {
      double myTop = this-&gt;GetY();
      double myLeft = this-&gt;GetX();
      double myRight = myLeft + this-&gt;GetWidth();
      double myBottom = myTop + this-&gt;GetHeight();
      double rTop = rectangle.GetY();
      double rLeft = rectangle.GetX();
      double rRight = rLeft + rectangle.GetWidth();
      double rBottom = rTop + rectangle.GetHeight();
  
      if(myTop &gt; rBottom) return false;
      if(myBottom &lt; rTop) return false;
      if(myLeft &gt; rRight) return false;
      if(myRight &lt; rLeft) return false;
  
      return true;
  }
  </code></pre>
  
  <p>A sample of the Contains methods with double dispatch:</p>
  
  <pre><code>//THIS RESULTS IN A STACK OVERFLOW! DUE TO INFINITE RECURSION!
  bool Rectangle::Contains(const Shape&amp; shape) const {
      return this-&gt;Contains(shape);
  }
  
  //THIS IS NOT TRUE IN ALL CASES!
  bool Rectangle::Contains(const Shape&amp; shape) const {
      return shape.Contains(*this);
  }
  
  
  bool Rectangle::Contains(const Point&amp; point) const {
      return this-&gt;Intersects(point);
  }
  
  bool Rectangle::Contains(const Line&amp; line) const {
      return this-&gt;Intersects(line.GetPointOne()) &amp;&amp; this-&gt;Intersects(line.GetPointTwo());
  }
  
  bool Rectangle::Contains(const Rectangle&amp; rectangle) const {
      return this-&gt;Intersects(rectangle.GetTopLeft()) &amp;&amp; this-&gt;Intersects(rectangle.GetTopRight()) &amp;&amp; this-&gt;Intersects(rectangle.GetBottomLeft()) &amp;&amp; this-&gt;Intersects(rectangle.GetBottomRight());
  }
  </code></pre>