   Run-time interpretation of overloaded operators
  ElloIm messing around with polymorphism and operator overloading, and ive run into a problem.  Say i have 2 classes declared as   class box{public:box(int len,int hei, int dep);box(const box& boxcopy);~box();virtual int volume();bool operator==(const box& boxcomp);protected:int length;int height;int depth;char* pchar;};class cornbox : public box{   public:    cornbox(int len, int hei, int dep);    ~cornbox();    int volume();    bool operator==(const cornbox& boxcomp); private: char* pmessage; };Notice the 2 overloaded versions of ==.  Is there a way to make the cornbox operator== be called from a base class pointer which is pointing to a cornbox instance? .. i.e. box* pbox = &cornboxinstance if(*pbox==anothercornboxinstance)do somethingWhen i try to do this, the base class version of == always gets called.  Ive tryed declaring them both as virtual, yet the base class one still gets called.  So is there a way to make the above code call the correct derived class version of == when comparing 2 derived class objects through pointers or references?hope that makes sense  thx 
  Hm, I guess it's because operator== is not virtual in the base class, thus your problem is that the compiler only knows pbox is a box*, not a cornbox*One solution, which I guess will be unsatisfactory, is:if( *(dynamic_cast< cornbox* >(pbox)) == anothercornboxinstance)  Another recommendation:  if you can't make operator== virtual (I forget if this is legal), then make your own virtual bool compare() method and forgoe operator overloading.  Often it just leads to confusion anyway.Regards,JeffEdit:  stupid tags ate my angle brackets...   [edited by - rypyr on August 1, 2003 4:37:32 PM];  First, yes, make the operator== virtual in the base and derived classes. Then, in class cornbox, you need to add this operator:virtual bool operator==(const box& boxcomp);That way, you''re actually overriding the correct operator in class box. When you just putvirtual bool operator==(const cornbox& boxcomp);you aren''t actually overriding. That''s a completely separate operator== from what''s in class box. ;quote:Original post by fizban75  First, yes, make the operator== virtual in the base and derived classes. Then, in class cornbox, you need to add this operator:virtual bool operator==(const box& boxcomp);That way, you''re actually overriding the correct operator in class box. When you just putvirtual bool operator==(const cornbox& boxcomp);you aren''t actually overriding. That''s a completely separate operator== from what''s in class box.   Still doesnt help, it just calls the derived version of virtual bool operator==(const box& boxcomp); and not the version of == comparing 2 derived classes which i would expect.;  You shouldn''t expect that though, because in your code, you''re not comparing a cornbox to a cornbox. You''re actually comparing a box to a box, because you''re using a class box on the left side and class box doesn''t know anything about class cornbox and therefore doesn''t have a function that allows it to be compared against cornboxes. Because there''s no function in the base class comparing boxes to cornboxes, the only thing you can do with a box reference is compare it to other boxes.If you''d like, you can at try to dynamically cast the box to a cornbox in the cornbox comparison function, but I actually think you should redesign it so you aren''t using box pointers or references to compare two cornboxes.Polymorphism happens when a derived class specializes the functionality in a base class. However, the sticking point is that the derived class function only gets as much information as was available to the base class function, and in this case, because the base function only knows how to compare against other boxes, the only specialization you can do with a derived class is also compare it against other boxes. ;  Thx for the info, adding a version of the derived class operator to the base class seems to allow polymorphism with the operator(virtual bool operator==(const cornbox& boxcomp2)).  Although this does mean writing out a function for the base class which i think will never be used, which is a bit fugly.   [edited by - quant on August 1, 2003 5:28:24 PM];  Btw, what''s a cornbox? ;  What you're asking for is called multimethods, among other names. Multimethods aren't supported by C++. The idea is that for any function (operators or normal functions) the type of a variable should be checked at runtime to determine which function to call. Most languages, including C++, only do that for the "this" argument, the object that owns the function. For the rest of the arguments it uses the known type at compile time to determine which function to call.A workaround/hack is to do a runtime check for yourself using RTTI. Something like: class baseclass{  bool operator== (baseclass baseinstance);}class derivedclass{  bool operator== (derivedclas derivedinstance);  bool operator== (baseclass baseinstance) {    if basinstance is of type derivedclass derivedclass copy = (derivedclass)baseinstance return this == copy // calls the other operator==} [edited by - Dobbs on August 2, 2003 2:01:48 AM];quote:Run-time interpretation of overloaded operatorsThere is none.Signature matching is performed using the static types at compile time.;  You need to make your base class destructor virtual too.It won''t affect operator==, but must be done nonetheless.   [ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ][ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ] 