2

I was thinking about how to improve my simple calculator using some advanced techniques. I came to question, is there some way to create a class with function you could define per instance:

class Function
{
public:
    Function(function);
    ~Function();

private:
    function;
};

So for example you create an instance

Function divide(int x / int y); //For example

I hope you understand the question.

EDIT:

So I studied the void (*foo)(int) method. It could be used. But the initial idea was to create a generic function that holds the function itself in it. Not just a pointer to a function defined outside. So you could do something like this:

int main() {

//Define the functions
Function divide( X / Y ); //Divide
Function sum( X + Y ); //Sum

//Ask the user what function to call and ask him to enter variables x and y

//User chooses divide and enters x, y 
cout << divide.calculate(x, y) << endl;

return 0;
}

Answer: @Chris Drew pointed out:
Sure, your Function can store a std::function<int(int, int)> and then you can construct Function with a lambda: e.g: Function divide([](int x,int y){return x / y;}); But then I'm not sure what your Function offers that you can't just do with std::function.

It answers my question, unfortunately my question was put on hold so I cannot mark the question resolved.

  • You mean something like a function pointer: `int (*function)(int,int))`? – πάντα ῥεῖ Jun 21 '15 at 12:36
  • What's your intention? Could you provide more examples? – dlask Jun 21 '15 at 12:39
  • @πάνταῥεῖ yes something like this –  Jun 21 '15 at 12:40
  • 1
    `std::function` may help also for binery function. – Jarod42 Jun 21 '15 at 12:42
  • 2
    possible duplicate of [Should I use std::function or a function pointer in C++?](http://stackoverflow.com/questions/25848690/should-i-use-stdfunction-or-a-function-pointer-in-c) – NathanOliver Jun 21 '15 at 12:44
  • Sure, your `Function` can store a `std::function` and then you can construct `Function` with a lambda: e.g: `Function divide([](int x,int y){return x / y;});` But then I'm not sure what your `Function` offers that you can't just do with `std::function`. – Chris Drew Jun 21 '15 at 13:29
  • @ChrisDrew Well that explains how to do it. Thanks! Could you write an answer so I can mark it as resolved? –  Jun 21 '15 at 13:37
  • @BBPP20Development I'd love to but your question is "on hold" because it was deemed to be unclear what you were asking. Perhaps now that you have added an edit explaining your intentions it will be reopened. – Chris Drew Jun 21 '15 at 13:40
  • @ChrisDrew I already edited it several times. Shame. I will mention the answer in my next edit. –  Jun 21 '15 at 13:42

1 Answers1

4

Sure, your Function can store a std::function<int(int, int)> and then you can construct Function with a lambda:

#include <functional>
#include <iostream>

class Function {
  std::function<int(int, int)> function;
public:
  Function(std::function<int(int, int)> f) : function(std::move(f)){};
  int calculate(int x, int y){ return function(x, y); }
};

int main() {
  Function divide([](int x, int y){ return x / y; });
  std::cout << divide.calculate(4, 2) << "\n";  
}

Live demo.

But then, as it stands, I'm not sure what Function offers that you can't do with a std::function directly:

#include <functional>
#include <iostream>

using Function = std::function<int(int, int)>;

int main() {
  Function divide([](int x, int y){ return x / y; });
  std::cout << divide(4, 2) << "\n";  
}

Live demo.

Chris Drew
  • 14,926
  • 3
  • 34
  • 54
  • Using function<> as member has the advantage of polymorphic behavior by switching the function of the same interface but with different implementation (could be at construction time, or at runtime). For example draw() from a shape object. the shape could be triangle or circle. This happens in my design in certain projects (you don't want to start a new derived class, just want to use a different method) – Kemin Zhou Oct 06 '20 at 19:44