0

This is the class that contains the overloaded function operator:

template < typename KeyType=int >
class Less {
  public:
    bool operator()(const KeyType &a, const KeyType &b) const { return a < b; }
  };

How can I use this? This class is specified in a header that contains a class specification for a heap ADT as well. I am trying to use it in one of my heap ADT's member functions, and am not sure of the syntax. I assumed it would be as follows:

if(Less<KeyType>::(param1, param2)){
...

But the compiler gives me an error: expected unqualified-id before '(' token

It DOES work like this:

if(Less<KeyType>::operator()(param1, param2)){
...

But there has to be a way to use it in another way that is less cluttered. If I wanted it to look like this, I wouldn't have overloaded an operator in the first place and would have just made it a typical function.

I've tried doing some research on this question before asking here, but it's a little hard to find an answer to something this specific. My searches keep leading me to different topics.

Bobazonski
  • 1,515
  • 5
  • 26
  • 43

1 Answers1

1

This operator is a non-static member function. So you need to create an object of type Less to call it. For example

if ( Less<>()( param1, param2 ) ) { /*...*/ }

Or

if ( Less<SomeType>()( param1, param2 ) ) { /*...*/ }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I see... but I want to be sure I fully understand this. Is your solution essentially declaring an object of type `Less` and using its member function, all in one line? That is, `Less<>()` is declaring an object? – Bobazonski Apr 09 '14 at 02:12
  • 1
    @Bobazonski Less<>() is a call of the default constructor that to create a temporary object and then to call the operator function. – Vlad from Moscow Apr 09 '14 at 02:15