operator overloading and inheritance
- by user168715
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.