I have an AVL tree which uses templates and assumes that the node objects are comparable, so it compares them directly, rather than comparing some kind of key associated with the objects:
void insert( const Comparable & x, AvlNode * & t )
{
if( t == nullptr )
t = new AvlNode( x, nullptr, nullptr );
else if( x < t->element )
insert( x, t->left );
else if( t->element < x )
insert( x, t->right );
balance( t );
}
In order for this to work, I implemented an overloaded < operator in my class, which uses a member variable of the class to compare the two objects:
bool operator <(const myClass & myObject) const
{
return myVariable < myObject.myVariable;
}
This works perfectly when I create an AVL tree of objects:
AvlTree<myClass> myTree;
However, it does not work when I create an AVL tree of pointers to objects:
AvlTree<myClass*> myTree;
The comparisons inside the tree seem to compare the addresses of the pointers, rather than the member variables. I tried implementing a similar overloaded < operator in my class for pointers:
bool operator <(const myClass *& myObject) const
{
return myVariable < myObject->myVariable;
}
But the comparisons ignore my overloaded operator and still use the addresses of the pointers. Is there any way to force the comparisons to use my operator, just as they do with normal objects?