How can I fix this TEMPLATE class?

N

Nobody

I wrote an AVL Tree template class... its all working except for every I did
something like:

if (element < newElement)
else if (element > newElement)

and so on, obviously that'll work for every type that has an overriden <, >
and = operator... but wont work for stuff like char*, it'll compare the
address instead of the actual data...

I'm assuming the correct way to fix this is to provide some way for the user
to pass in a comparison function? what is a clean way to do that?
 
A

Alan Johnson

Nobody said:
I wrote an AVL Tree template class... its all working except for every I did
something like:

if (element < newElement)
else if (element > newElement)

and so on, obviously that'll work for every type that has an overriden <, >
and = operator... but wont work for stuff like char*, it'll compare the
address instead of the actual data...

I'm assuming the correct way to fix this is to provide some way for the user
to pass in a comparison function? what is a clean way to do that?

Make the comparison function a template parameter, and make it default
to std::less. See std::map for an example:
http://www.sgi.com/tech/stl/Map.html

Note also that probably the only operation you need is "less than", and
you should be able to build any others you need out of that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top