How can I make a class method that takes a function as a parameter?

John Bardeen

I want to create a method inside a class (but I think this is irrelevant) that sorts a vector according to the bool value returned by a particular function... I read that it is possible but I don't know how to do it...

I would like to call the function and put (for example) >, or <, as a parameter, so that I don't have to create two almost identical functions.

How I imagine the body of the function:

for (int i = 0 ; i < DIM - j; i++)
{
    if (some_function(v[i], v[i+1]))
    {
        int temp;
        temp = v[i];
        v[i] = v[i+1];  
        v[i+1] = temp;
    }
}
cigien

You can do this by passing in a function object:

template <typename Op>
void f(Op op) {
  // ...
  if (op(v[i], v[i+1])) // ...
}

and call it like this:

f(std::less{});    // for <
f(std::greater{}); // for >

Of course, f here is just an example, but you just need to add a template parameter to your own function in this way.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I make a function that takes in a parameter and when executed, it alerts whatever you give the function?

How can you make a function that takes as a parameter an arbitrary function pointer?

How to make a function in a base class that takes in a subclass of itself as a parameter?

How can I make a templated function that takes a parameter that is the result of boost::bind?

How can I make parameter in method valid?

How can I stub a method that takes a Generic class as a param?

How can I add a method as a parameter of a function

How can I give a class as a parameter to function?

Can I make a utility method which takes an arbitrary expression as a parameter and calls it?

How can I make this refers to the class in the function?

How can I pass a class method as a parameter to another function and later call it, preferably making the variable class method signature explicit?

How can I make a function that takes a function as an argument and returns a modified version of that function?

How can I make a function parameter respect an object type in TypeScript?

How can I make a function pointer parameter no-op by default?

How can i patch out a parameter from a function that calls a method?

How can I pass method as function parameter without 'lambda' keyword?

How can I write a function in Golang which takes multiple types as a parameter?

How can I define virtual function which takes derived type as its parameter?

How can I create a function in Apps Script that takes a String as a parameter and then return information specific to that String?

How can I make a method accept a class type variable in Java?

How can i make a method available only from within the class

How can I create a class whose object takes a function in the constructor and can execute it?

How can I use vector of an abstract/interface class as function parameter?

How can I write a function that expect a class as parameter with a private constructor

How can i pass class as a parameter to a function in Swift?

How to make a default argument to parameter which takes function address?

How do you use a method that takes a function as parameter in Scala?

How can I make a function which takes the arguments of one of it's arguments in Typescript?

How can I make a class decorator not break isinstance function?

TOP Ranking

HotTag

Archive