TITLE: Binary operators with derived classes (multiple dispatch) PROBLEM: Eric GoodMan (ericg@ucschu.ucsc.edu) With binary operators of unknown actual type only the first object gets run time determined. To test type compatibility for two arbitrary base class references there is no way to determine whether or not they are "the same" other than that they are derived from the same class. RESPONSE: Brian Beuning There was an article in a recent Journal of OOP (JOOP) about supporting arithmetic in C++ that got into this topic. One suggestion was to use: class B { public: virtual B& operator+( B& ); virtual B& add_to_D( class D& ); virtual B& add_to_E( class E& ); }; class D: public B { public: B& operator+( B& ); B& add_to_D( D& ); // D + D B& add_to_E( E& ); // E + D }; class E: public B { public: B& operator+( B& ); B& add_to_D( D& ); // D + E B& add_to_E( E& ); // E + E }; B& D::operator+( B& arg ) { return( arg.add_to_D( *this ) ); // second run-time lookup } B& E::operator+( B& arg ) { return( arg.add_to_E( *this ) ); } If you have N derived classes, you end up with about N^2 methods. But you can do it. It is also extendable to more than 2 arguments. [ Note that one downside to this is also that the base class gets contaminated with knowledge of all descendents. Additionally sibling classes get contaminated. This general problem shows up in many places and is often rejected as an unacceptable design. The alternative to multiple dispatch is to have run-time type testing. -adc ]