operator overloading and inheritance

Posted by user168715 on Stack Overflow See other posts from Stack Overflow or by user168715
Published on 2010-06-09T21:01:19Z Indexed on 2010/06/09 21:22 UTC
Read the original article Hit count: 156

Filed under:
|

I was given the following code:

class FibHeapNode
{
     //...

     // These all have trivial implementation
     virtual void operator =(FibHeapNode& RHS);
     virtual int  operator ==(FibHeapNode& RHS);
     virtual int  operator <(FibHeapNode& RHS);

};

class Event : public FibHeapNode
{
     // These have nontrivial implementation
     virtual void operator=(FibHeapNode& RHS);
     virtual int operator==(FibHeapNode& RHS);
     virtual int operator<(FibHeapNode& RHS);

};

class FibHeap
{
     //...

     int  DecreaseKey(FibHeapNode *theNode, FibHeapNode& NewKey)
     {
          FibHeapNode *theParent;

          // Some code

          if (theParent != NULL && *theNode < *theParent)
          {
            //...
          }

          //...
          return 1;
     }
};

Much of FibHeap's implementation is similar: FibHeapNode pointers are dereferenced and then compared.

Why does this code work? (or is it buggy?) I would think that the virtuals here would have no effect: since *theNode and *theParent aren't pointer or reference types, no dynamic dispatch occurs and FibHeapNode::operator< gets called no matter what's written in Event.

© Stack Overflow or respective owner

Related posts about c++

Related posts about operator-overloading