Skip to main content
added 22 characters in body; deleted 19 characters in body
Source Link
Nav
  • 20.4k
  • 28
  • 96
  • 141

I found this code on Wikipedia

class compare_class {
  public:
  bool operator()(int A, int B) const {
    return A < B;
  }
};
...
// Declaration of C++ sorting function.
template <class ComparisonFunctor> 
void sort_ints(int* begin_items, int num_items, ComparisonFunctor c);
...
int main() {
    int items[] = {4, 3, 1, 2};
    compare_class functor;
    sort_ints(items, sizeof(items)/sizeof(items[0]), functor);
}

At first I wondered how the A and B parameters got passed to operator()(int A, int B), when the functor was mentioned in sort_ints without even any parenthesis.

Then I figured that A and B are being passed to the function object inside the sort_ints function. But then, shouldn't the declaration of sort_ints have 'ComparisonFunctor***** c' instead of 'ComparisonFunctor c', since it's receiving the address of a function?

Inside the sort_ints function, would the function call to the functor be done something like this?

functor(*begin_items, *(begin_items+1));

I found this code on Wikipedia

class compare_class {
  public:
  bool operator()(int A, int B) const {
    return A < B;
  }
};
...
// Declaration of C++ sorting function.
template <class ComparisonFunctor> 
void sort_ints(int* begin_items, int num_items, ComparisonFunctor c);
...
int main() {
    int items[] = {4, 3, 1, 2};
    compare_class functor;
    sort_ints(items, sizeof(items)/sizeof(items[0]), functor);
}

At first I wondered how the A and B parameters got passed to operator()(int A, int B), when the functor was mentioned in sort_ints without even any parenthesis.

Then I figured that A and B are being passed to the function object inside the sort_ints function. But then, shouldn't the declaration of sort_ints have 'ComparisonFunctor***** c' instead of 'ComparisonFunctor c', since it's receiving the address of a function?

Inside the sort_ints function, would the function call to the functor be done something like this?

functor(*begin_items, begin_items+1);

I found this code on Wikipedia

class compare_class {
  public:
  bool operator()(int A, int B) const {
    return A < B;
  }
};
...
// Declaration of C++ sorting function.
template <class ComparisonFunctor> 
void sort_ints(int* begin_items, int num_items, ComparisonFunctor c);
...
int main() {
    int items[] = {4, 3, 1, 2};
    compare_class functor;
    sort_ints(items, sizeof(items)/sizeof(items[0]), functor);
}

At first I wondered how the A and B parameters got passed to operator()(int A, int B), when the functor was mentioned in sort_ints without even any parenthesis.

Then I figured that A and B are being passed to the function object inside the sort_ints function. But then, shouldn't the declaration of sort_ints have 'ComparisonFunctor***** c' instead of 'ComparisonFunctor c', since it's receiving the address of a function?

Inside the sort_ints function, would the function call to the functor be done something like this?

functor(*begin_items, *(begin_items+1));
Source Link
Nav
  • 20.4k
  • 28
  • 96
  • 141

Help understanding the working of Function Objects?

I found this code on Wikipedia

class compare_class {
  public:
  bool operator()(int A, int B) const {
    return A < B;
  }
};
...
// Declaration of C++ sorting function.
template <class ComparisonFunctor> 
void sort_ints(int* begin_items, int num_items, ComparisonFunctor c);
...
int main() {
    int items[] = {4, 3, 1, 2};
    compare_class functor;
    sort_ints(items, sizeof(items)/sizeof(items[0]), functor);
}

At first I wondered how the A and B parameters got passed to operator()(int A, int B), when the functor was mentioned in sort_ints without even any parenthesis.

Then I figured that A and B are being passed to the function object inside the sort_ints function. But then, shouldn't the declaration of sort_ints have 'ComparisonFunctor***** c' instead of 'ComparisonFunctor c', since it's receiving the address of a function?

Inside the sort_ints function, would the function call to the functor be done something like this?

functor(*begin_items, begin_items+1);