Path: utzoo!utgpu!water!watmath!clyde!rutgers!mcnc!thorin!grant!haskell From: haskell@grant.cs.unc.edu (Phyllis Haskell) Newsgroups: comp.lang.c++ Subject: use of overloaded operators with derived classes? Message-ID: <656@thorin.cs.unc.edu> Date: 12 Jan 88 17:58:37 GMT Sender: news@thorin.cs.unc.edu Lines: 49 We're having a problem trying to use overloaded operators with derived classes. A sample program follows in which we are attempting to pass a derived class to the routine "test", which we want to multiply the instances of any derived class. We want "test" to be a generic routine which does not need to "worry" about the types of arguments used in its invocation. Is there a way to do this? #include class Datatype { public: virtual Datatype* newobj() {fprintf (stderr,"newobj for base\n");} virtual void dump() { fprintf (stderr," dump undefined for base class\n");} }; class Dint: public Datatype { int temp; public: Dint () {} Dint (int in) {temp = in;} Dint& operator* (Dint& in) { Dint& newvar = *new Dint; newvar.temp = temp * in.temp; return newvar; } Datatype* newobj() {fprintf (stderr,"newobj for Dint\n"); return new Dint;} void dump () { fprintf (stderr, "Dint = %d\n", temp); } }; void test(Datatype& in, Datatype& in2) { Datatype* temp = in.newobj(); // create an instance of a subclass ?? /* (*temp) = in * in2; // here's our problem - how do we get proper // * operator for subclass. (commented to prevent // compiler error) */ temp->dump(); } main () { Dint inta(5), intb(6); test(inta, intb); }