Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!ames!pacbell.com!att!cbnewse!cbnewsd!bgbg From: bgbg@cbnewsd.att.com (brian.g.beuning) Newsgroups: comp.lang.c++ Subject: Re: Seeking neat way to do binary "virtual" functions. Message-ID: <1991Mar24.045917.29264@cbnewsd.att.com> Date: 24 Mar 91 04:59:17 GMT References: <13683@darkstar.ucsc.edu> Distribution: comp Organization: AT&T Bell Laboratories Lines: 45 From article <13683@darkstar.ucsc.edu>, by ericg@ucschu.ucsc.edu (Eric Goodman): > ... 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. 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. Brian Beuning