1

I have a function

float addXPercent (float percent, ExampleClass* A, ExampleClass B, thisFunction) {
  return (percent * B.thisFunction() + (*A).thisFunction());
}

and someFunction is a function that can be applied to both A and B like this

(*A).setSomething(0.4 * B.someFunction() + (*A).someFunction());

It takes a percent value like 0.4, applies it to the result of B.someFunction() and adds that value to the current value of A.someFunction().

What I want to do is generalize this part of the function out:

0.4 * B.someFunction() + (*A).someFunction()

into that addXPercent function. The result I expect to look something like this

(*A).setSomething( addXPercent (0.3, *A, B, thisFunction) );

I'm quite new to C++ so I did some googling and found out you can pass function to different function with the std header <functional>.

But I'm not sure how I'll pass a member function with syntax like this function< void () > thisFunction.

How should I go about doing that?

I wasn't able to find a related thread 1 2 (got Sorry, there were no post results for “”) so sorry if this is a duplicate question.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • 1
    Please don't split up your code into a bunch of one liners. Put it all together in something that would compile except for the part specific to your question and then cobble together a line showing what you'd like to do. – xaxxon Apr 03 '19 at 21:31
  • 3
    (Off-topic) Use `A->someFunction()` instead of `(*A).someFunction()`. The `->` operator was invented just for that purpose :-) – Nikos C. Apr 03 '19 at 21:32
  • @NikosC. somewhat funnily, he'll probably need to use both at the same time soon.. `->*` – xaxxon Apr 03 '19 at 21:33
  • https://stackoverflow.com/questions/51245152/how-to-pass-member-function-as-a-parameter – Tas Apr 03 '19 at 21:35
  • You might be looking for `std::mem_fn`: https://en.cppreference.com/w/cpp/utility/functional/mem_fn – UnholySheep Apr 03 '19 at 21:35

2 Answers2

0

What I want to do is generalize this part of the function out

You can do this with pointer to member function:

float addXPercent (float percent, ExampleClass* A, ExampleClass B, float (ExampleClass::*someFunction)()) {
  return (percent * (B.*someFunction)() + (A->*someFunction)());
}

You can call addXPercent like this:

ExampleClass a, b;

addXPercent(0.3f, &a, b, &ExampleClass::thisFunction);
geza
  • 28,403
  • 6
  • 61
  • 135
0

I'm not sure I fully understand your intentions. As far as I know passing functions as parameters can be archived with function pointers, Functors or Lambdas in c++11. For example bellow there's some approach using functors that maybe you'll find useful.

class ExampleClass 
{
    float something;

    public:

    void setSomething(float val)
    {
        something = val;
    }

    struct ThisFunction 
    {
      float operator()(ExampleClass &example) {
        // Do whatever you need here
        float result = 2.0;
        return result;
      }
    };

};



float addXPercent (float percent, ExampleClass *A, ExampleClass B, ExampleClass::ThisFunction &thisFunction) {
  return ((percent * thisFunction(B)) + thisFunction((*A)));
}

int main() 
{
    ExampleClass A;
    ExampleClass B;

    ExampleClass::ThisFunction thisFunction;

    A.setSomething(addXPercent (0.3, &A, B, thisFunction));
};
Pelayo Méndez
  • 389
  • 4
  • 10