Path: utzoo!mnetor!uunet!husc6!mit-eddie!uw-beaver!tikal!hplsla!jima From: jima@hplsla.HP.COM (jim adcock ) Newsgroups: comp.lang.c++ Subject: Re: use of overloaded operators with derived classes? Message-ID: <6590006@hplsla.HP.COM> Date: 14 Jan 88 02:40:30 GMT References: <656@thorin.cs.unc.edu> Organization: HP Lake Stevens, WA Lines: 118 /* If I understand your test constraints correctly, this is as close as I can figure out to meeting your requirements. The general idea is that in each derived class Dxxxx, derived from Datatype, there would be two operator* methods: virtual Datatype& operator*(Datatype& in) and [non-virtual] Dxxxx& operator*(Dxxxx& in) where the second (non-virtual) method would provide a fast in-line method to be used between two objects of class Dxxxx for "real" programming usage, and the first method provides a "virtual" interface to the second method, to be called by the test routine. Unfortunately, allowing overloading of methods NOT defined in the base class does not seem to be implemented in the present AT&T compiler. (See what happens when you change operator_mpy to operator* in all places where operator_mpy is used -- the AT&T compiler says: "Sorry, we haven't implemented this feature !!!") So, my workaround was to come up with the "fake" name "operator_mpy" for the virtual version of the "operator*" function. *** SIGH *** It will be nice when we get a "finalized" C++ compiler !!! PS: The approach taken puts a "hidden" pointer to the virtual function dispatch table in each object. It's unclear from your question whether this would be acceptible overhead or not. If not, the only solution I would have is to put the virtual/test functions in each class inside an #ifdef TEST block, so that they would not be compiled in the final release of your software. (The pointer to the virtual dispatch table is placed in an object ONLY if it or its superclasses uses virtual functions) */ #include class Datatype { public: Datatype() {}; virtual const char* isA() {return "Datatype";}; virtual void dump() { fprintf (stderr,"no value to dump in Datatype\n");}; virtual Datatype& /* operator* */ operator_mpy(Datatype& in) { fprintf(stderr,"Datatype operator* incorrectly used\n"); exit(1); return *this; }; }; class Dint: public Datatype { int temp; public: Dint () {}; Dint (int in) {temp = in;}; virtual const char* isA() { return "Dint";}; virtual Datatype& /* operator* */ operator_mpy(Datatype& in); Dint& operator* (Dint& in) { Dint& newvar = *(new Dint); newvar.temp = temp * in.temp; return newvar; } void dump () { fprintf (stderr, "Dint = %d\n", temp); } }; virtual Datatype& /* operator* */ Dint::operator_mpy(Datatype& in) { if (in.isA() != isA()) { fprintf(stderr,"mismatch in Dint operator*\n"); exit(1); } return (*this) * (*(Dint*)(&in)); } class Dflt: public Datatype { float temp; public: Dflt () {}; Dflt (int in) {temp = (float)in;}; virtual const char* isA() { return "Dflt";}; virtual Datatype& /* operator* */ operator_mpy(Datatype& in); Dflt& operator* (Dflt& in) { Dflt& newvar = *(new Dflt); newvar.temp = temp * in.temp; return newvar; } void dump () { fprintf (stderr, "Dflt = %f\n", temp); } }; virtual Datatype& /* operator* */Dflt::operator_mpy(Datatype& in) { if (in.isA() != isA()) { fprintf(stderr,"mismatch in Dflt operator*\n"); exit(1); } return (*this) * (*(Dflt*)(&in)); } void test(Datatype& in, Datatype& in2) { Datatype* temp = &(in.operator_mpy(in2)); temp->dump(); } main () { Dint inta(5), intb(6); Dflt flta(5), fltb(6); test(inta, intb); test(flta, fltb); }