function that takes type function

Jan 18, 2018 at 9:29pm
i'm looking to create something like this but i don't know how to pass a function to another function and i cant figure it out from anything online

void function(){
25 * var +21
}

class new {
int var = 20;
public:
int output;
void process(void a){
output = a;
}
};
int main(){
new n;
n.process(function);
cout << n.output << endl;
}
Jan 18, 2018 at 9:48pm
here you could use a function reference/pointer, std::function, or a template:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <functional>
#include <iostream>
int fun(int var) { return 25 * var + 21; }
class New { 
  int var = 20;
public:
  int output;
  // #1: reference to function (most specific)
  void process1(int(&a)(int)) {  output = a(var); }
  // #2: pointer to function (C-style)
  void process2(int(*a)(int)) {  output = a(var); }
  // #3: std::function (generic, has overhead)
  void process3(std::function<int(int)> a) { output = a(var); }
  // #4: template (more generic, no overhead)
  template<class F>
  void process4(F a) { output = a(var); }
};
int main(){
  New n;
  n.process1(fun);
  n.process2(fun);
  n.process3(fun);
  n.process4(fun);
  std::cout << n.output << '\n';
}

live demo https://wandbox.org/permlink/VJLhZ5cMQD1jZWL9
Jan 18, 2018 at 9:50pm
You're referring to the use of function pointers. Function pointer syntax can be ugly.
In C++, people usually prefer std::function objects, or polymorphism.

It would help if you posted code that was a bit more concrete, I'm not exactly sure what you're doing there. Your function() doesn't actually do anything. And "new" can't be the name of a class in C++ because it's a keyword. I would suggest just getting used to how functions work before you tackle function pointers.

But here's a nice tutorial for how to use function pointers
https://www.cprogramming.com/tutorial/function-pointers.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Example program
#include <iostream>

int function(int var){
    return 25 * var + 21;
}

class my_class { 

  public:
    int output;
    
    my_class()
    : output(0) {}
    
    // process a function that takes in an int, and returns an int
    void process(int (*func)(int)) {
        output = func(var);
    }
    
  private:
    int var = 20;
    
};

int main() {
    my_class n;
    n.process(function);

    std::cout << n.output << std::endl;
}


Last edited on Jan 18, 2018 at 9:51pm
Jan 19, 2018 at 2:46am
I understand how they work and I know new cannot be used as a name for a function I just couldn't think of another name if you would like a snippet of the code I'm working on to get a better idea of what I'm doing I would be happy to provide it
Last edited on Jan 19, 2018 at 2:47am
Jan 19, 2018 at 3:54am
No that's okay, sorry if I sounded harsh. Let us know if you need more help.
Jan 19, 2018 at 12:23pm
You were fine thank you for your help
Last edited on Jan 19, 2018 at 12:23pm
Jan 19, 2018 at 6:34pm
if anyone could give me an example for what im trying to do that would be great ive been looking at the tutorials online and dont really understand because there trying to do somthing completely different than me it seems.
Jan 19, 2018 at 7:48pm
ReaperSoul wrote:
if anyone could give me an example for what im trying to do


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;


int square( int x ) { return x * x;  }        // Some example
int triple( int x ) { return 3 * x;  }        //   functions of the
int subt10( int x ) { return x - 10; }        //   required form



class Thing
{
   int var;
public:
   Thing( int v ) { var = v; }
   int getVar() { return var; }
   void process( int (*f)(int) ) { var = f( var ); }    // Either of these
// void process( int f(int) )    { var = f( var ); }    //   will work
};



int main()
{
   Thing n( 2 );             cout << "Start : var = " << n.getVar() << endl;

   n.process( triple );      cout << "Triple: var = " << n.getVar() << endl;

   n.process( square );      cout << "Square: var = " << n.getVar() << endl;

   n.process( subt10 );      cout << "Sub 10: var = " << n.getVar() << endl;
}


Start : var = 2
Triple: var = 6
Square: var = 36
Sub 10: var = 26


This is "pointers to functions". @Cubbi's list is much more comprehensive.
Last edited on Jan 19, 2018 at 7:48pm
Topic archived. No new replies allowed.