My question is a bit about programming techniques or, maybe, design patterns. Suppose several classes derived from base which contains a pure virtual method that should do some interactions beetwen them.
For instance, Rectangle, Ellipse, Triangle and Line – all derived from a Shape. And this abstract class, Shape, contains virtual bool Intersects(Shape* another) = 0.
It seems, that I need to make six implementations, right (btw, is any better solution in this particular example?).
I have no idea of any other examples at the moment.
Maybe I’m talking about a thing that is well-known and I’m nearly sure there are some names describing the technique. However, I don’t even know what to input to find it in the Internet.
Well, can you tell me how to implement such thing (I’m still wondering if any helper method is needed, or, maybe, RTII‘s dynamic_cast?) or point some sources (arts, tutorials or whatever) about it?
This is the school book example of double dispatch, the wikipedia article gives a good description of the problem and the solution:
http://en.wikipedia.org/wiki/Double_dispatch
If I remember correctly there’s a very elegant solution to the problem in the book “Modern C++ Design” by Andrei Alexandescu
http://www.amazon.com/Modern-Design-Generic-Programming-Patterns/dp/0201704315