Tutorial confusion

On this website, there is a c++ tutorial as many of you know. I don't know if the examples it provides are relatively simple, but I am having trouble understanding 2 of them:

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
// pointer to functions
#include <iostream>
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))
{
  int g;
  g = (*functocall)(x,y);
  return (g);
}

int main ()
{
  int m,n;
  int (*minus)(int,int) = subtraction;

  m = operation (7, 5, addition);
  n = operation (20, m, minus);
  cout <<n;
  return 0;
}

This is found on http://www.cplusplus.com/doc/tutorial/pointers/ (a lesson on void pointers).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// increaser
#include <iostream>
using namespace std;

void increase (void* data, int psize)
{
  if ( psize == sizeof(char) )
  { char* pchar; pchar=(char*)data; ++(*pchar); }
  else if (psize == sizeof(int) )
  { int* pint; pint=(int*)data; ++(*pint); }
}

int main ()
{
  char a = 'x';
  int b = 1602;
  increase (&a,sizeof(a));
  increase (&b,sizeof(b));
  cout << a << ", " << b << endl;
  return 0;
}

This one is found on the same page, but this is about pointers to functions.

Could someone explain each step of how each program executes? If you could explain either one or the other, that would also be fine.
It would be easier if you ask what you don't understand.
Sorry...there is no specific question at all, and I don't understand it at all, but here are the specifics anyway:

Void Functions
1) int (*minus)(int,int) = subtraction; (line 21). So int (*minus) is a pointer, but what is (int,int) there for? And why set it equal to subtraction, not addition? And what does it point to and what is it used for? (--sorry, there's a lot here--)
2) m = operation (7, 5, addition); (line 23). This must send the values of "m" and "n" to "a" and "b," but when returned, do the values of "m" and "n" change to this value? But most importantly, how can we call function operation AND addition? (--This is the same for line 24--)
3)
1
2
3
4
5
6
int operation (int x, int y, int (*functocall)(int,int))
{
  int g;
  g = (*functocall)(x,y);
  return (g);
}

I have no clue what is going on in there, including that pointer and another double int.


Pointers to Functions
1)
1
2
3
4
5
6
7
8
9
int main ()
{
  char a = 'x';
  int b = 1602;
  increase (&a,sizeof(a));
  increase (&b,sizeof(b));
  cout << a << ", " << b << endl;
  return 0;
}

So we declare char a (x) and int b (1602) (--these are variables, right?--). Then we send (&a,sizeof(a)) to the void function "increase":
1
2
3
4
5
6
7
void increase (void* data, int psize)
{
  if ( psize == sizeof(char) )
  { char* pchar; pchar=(char*)data; ++(*pchar); }
  else if (psize == sizeof(int) )
  { int* pint; pint=(int*)data; ++(*pint); }
}

And I'm stumped as to what happens when it's thrown in there.

I'm sorry it's so much, but help would be appreciated. A solution to any question or part of a question is fine.
First off, you have them mixed up. The first one is a function pointer and the second one is a void pointer. I'm heading to my buddies house, but as soon as I get a few minutes, I'll type up comments for each line as to what it does.

In the mean time, maybe someone can walk your through, step by step.
First, your titles are incorrect.
Your first code show function pointers
The second, void pointers

1_
1
2
typedef int (*function_pointer)(int,int);
function_pointer minus = subtraction;
minus is a pointer to a function.
You are making point to 'subtraction', so when you dereference it, you could call subtraction
(*minus)(54,42);//executes subtraction(54,42)

2_ The function is in no way special. Its behaviour will no be different to others so
m = operation (7, 5, addition); calls operation, passing 7, 5 and 'addition' as parameters.
The idea is for the third parameter to decide what to do.

3_ int operation (int x, int y, function_pointer function_to_call); ¿better?


Get a debugger and execute step by step if you are unsure of the flow.
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
33
34
35
36
37
38
39
40
41
42
// pointer to functions
#include <iostream>
using namespace std;
// Addition function that accepts two ints as parameters
int addition(int a, int b) {
   // Returns the value of the addition
   return (a + b);
}

// Subtraction function that accepts two ints as parameters
int subtraction(int a, int b) {
   // Returns the value of the subtraction
   return (a - b);
}

// Function that accepts two ints and a function pointer with
//    two ints as paramters
int operation(int x, int y, int (*functocall)(int, int)) {
   // Creates an int to hold the return value of the function pointer
   int g;
   // Calls the function pointer with the two ints and returns it to g
   g = (*functocall)(x, y);
   // returns g
   return (g);
}

int main() {
   // Creates two ints
   int m, n;
   // Creates a function pointer to the function subtraction
   int (*minus)(int, int) = subtraction;

   // m equals the value returned by operation
   //    The addition function is called by name
   m = operation(7, 5, addition);
   // n equals the value returned by operation
   //    The substraction function is called by the function pointer minus
   n = operation(20, m, minus);
   // Prints out the addition and subtraction done
   cout << n;
   return 0;
}


This is the C way to call functions. C++ supports an easier way to call functions, std::function.

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
33
34
35
36
37
38
39
// increaser
#include <iostream>
using namespace std;

// Increase function that accepts an unknown datatype pointer named data
//    and an int containing the size of data
void increase(void* data, int psize) {
   // Compares the size of the value of data and checks to see if it's a char
   if (psize == sizeof(char)) {
      // Creates a new character pointer (since data was a character)
      char* pchar;
      // Assign it to a casted character pointer of data
      pchar = (char*)data;
      // Increment the dereferenced pointer to data
      ++(*pchar);
   // Compares the size of the value of data and checks to see if it's a char
   } else if (psize == sizeof(int)) {
      // Creates a new character pointer (since data was an int)
      int* pint;
      // Assign it to a casted integer pointer of data
      pint = (int*)data;
      // Increment the dereferenced pointer to data
      ++(*pint);
   }
}

int main() {
   // a = 'x'
   char a = 'x';
   // b = 1602
   int b = 1602;
   // a now equals 'y'
   increase(&a, sizeof(a));
   // b now equals 1603
   increase(&b, sizeof(b));
   // Display the results
   cout << a << ", " << b << endl;
   return 0;
}


This is the C way of calling a function with the same name to do the same thing to two different functions. C++ has something called function overloading. You can create a function (with the same name) but different data types so you can do whatever you need to do to those variables.

Hope this helps.
Last edited on
I simplified the first one to the code below, and I can understand it now (I still don't get how function operation fit in here, so I deleted it and made a few changes and this code works just as well).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// pointer to functions
#include <iostream>
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int main ()
{
  int m,n;
  int (*minus)(int,int) = subtraction;

  m = addition(7, 5);
  n = minus(20, m);
  cout <<n;
  return 0;
}


Now this code I understood, and I added extra comments to the lines:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// each comment applies to the line below it
#include <iostream>
using namespace std;

// Increase function that accepts an unknown datatype pointer named data
//    and an int containing the size of data...since it's a void pointer
//    it can accept two types (char and int).
void increase(void* data, int psize) {
   // Compares the size of the value of data and checks to see if it's a char
    if (psize == sizeof(char)) {
      // Creates a new character pointer (since data was a character)
      char* pchar;
      // Assign it to a casted character pointer of data... This is the pointer
      //    that actually derefrences the the address of the char (because void
      //    couldn't do it).
      pchar = (char*)data;
      // Increment the dereferenced pointer to data...increases the value by one
      //    char.
      ++(*pchar);
    }
   // Compares the size of the value of data and checks to see if it's a char
    else if (psize == sizeof(int)) {
      // Creates a new character pointer (since data was an int)
      int* pint;
      // Assign it to a casted integer pointer of data...Again, this is the pointer
      //    that actually derefrences the the address of the int (because void
      //    couldn't do it).
      pint = (int*)data;
      // Increment the dereferenced pointer to data...increases the value by one
      //    int.
      ++(*pint);
    }
}

int main() {
   // a = 'x'
   char a = 'x';
   // b = 1602
   int b = 1602;
   // a now equals 'y' (after being fed through function "increase")
   increase(&a, sizeof(a));
   // b now equals 1603 (after being fed through function "increase")
   increase(&b, sizeof(b));
   // Display the results.
   cout << a << ", " << b << endl;
   return 0;
}


Output:
y, 1603
Your simplified version of the function pointers has the same idea. The concept behind function pointers is an abstract one at best. I used them for a library that I build. In theory, my library had no idea what function was going to be passed. This can pose a serious problem because my library needed to call that function when condition x was met. Like I said, this is the C way of doing it, C++ now uses functional.

I guess a good way to look at this example is to place it into context of a calculator program. You have one function, operation, that you call whenever you want to do a calculation. You generate a switch condition to handle the user's input of which can be addition, subtraction, multiplication, or division. Your switch statement ends up looking just like this:
1
2
3
4
5
6
7
8
9
10
11
switch (operand) {
   case '+':
      funcToCall = addition;
      break;
   case '-':
      funcToCall = subtraction;
      break;
   // etc.
}

operation(num1, num2, funcToCall);


In the above code, you no longer have to worry about using the right function to call something, they're correctly assigned in the switch statement. Let's say that you want add an option to do modulus division, %, or you want to handle binary math, such as bitwise operations, you just simply type up the function definition for each, add a condition to your switch statement and you're done.

To make it easier to understand, you might also have a very long function operation which does more than just the operation of the two values (let's say you're writing all of the information out to a file) and it's just easier to contain everything in that one function. When you pass a function pointer (funcToCall in my example), you are just making it easier on yourself later on.

If you still need more assistance, I can try to type this example up a little better for you to understand the benefits of why you would even want to use it. The tutorials here just give you an overview of a feature, it's really up to you on how to implement it.

I still have the link laying around somewhere from when I first learned about function pointers. It had me stumped for a few weeks. I'll even give you the link to my header file I created that uses the C++ functional. You'll start to see why it was important I learned how it worked.
1.) Okay. . .Could you type up the full code of your example please, so I can understand everything that is going on in there?

2.) Is this an example of a function pointer, or is this an example of the C++ functional? (or are they the same thing?I have no clue...) Basically, I would like to know what "it" refers to in:

If you still need more assistance, I can try to type this example up a little better for you to understand the benefits of why you would even want to use it.


3.) Could you please send me that link where you first learned about pointers? And also the header file that you created if you think it will help me understand (I don't know what it is)?

Don't take this as demanding, but I really am stuck here and I don't want my lack of understanding of pointers/pointers to functions/functionals to be inhibitory to my programming in the future. Thanks for being patient!
1)
Code:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>

// Function Prototypes
int addition(int a, int b) { return a + b; }
int subtraction(int a, int b) { return a - b; }
int multiplication(int a, int b) { return a * b; }
int division(int a, int b) { return ((b == 0) ? b : a / b); }

// Function that uses the function pointer named FuncToCall
void operation(int x, int y, int (*FuncToCall)(int a, int b));

int main() {
   // Declare our variables to hold the two numbers
   int num1, num2;
   // Declare our variable to hold the sign
   char operand;
   // Declare our function pointer variable
   int (*mathFunc)(int a, int b);

   do {
      std::cout << "Please enter your equation (1!1 to quit): ";
      std::cin >> num1 >> operand >> num2;

      switch (operand) {
            // Addition
         case '+':
            // Assign mathFunc to the addition function
            mathFunc = addition;
            break;
            // Subtraction
         case '-':
            // Assign mathFunc to the subtraction function
            mathFunc = subtraction;
            break;
            // Multiplication
         case '*':
            // Assign mathFunc to the multiplication function
            mathFunc = multiplication;
            break;
            // Division
         case '/':
            // Assign mathFunc to the division function
            mathFunc = division;
            break;
         default:
            continue;
      }
      // Call our operation function with the function pointer
      operation(num1, num2, mathFunc);
   } while (operand != '!');

   return 0;
}

void operation(int x, int y, int (*FuncToCall)(int a, int b)) {
   std::cout << "Answer is: " << FuncToCall(x, y) << "\n\n";
}

Please enter your equation (1!1 to quit): 2+5
Answer is: 7

Please enter your equation (1!1 to quit): 2-5
Answer is: -3

Please enter your equation (1!1 to quit): 5*7
Answer is: 35

Please enter your equation (1!1 to quit): 45/3
Answer is: 15

Please enter your equation (1!1 to quit): 1!1


This is a very basic example of how a simple calculator can be made using the function pointers. There is no error checking with the exception of dividing by zero and the quit option isn't the best either, but it allows the user to enter any integer. The calculator can be expanded upon to support doubles and other features.

2) These are all examples of a function pointer. Function pointers are a C thing. Instead of doing it this way (refer to above code), C++ has implemented the functional ability (refer below). They are, in a sense, the same thing. They act the same, but C++ has a "nicer" wrapper around it, depending on who you ask. I personally hate the way the C function pointers look so it was clear that I took on the C++ functional.

3) The link to my header file, which I built when I first took C++, is here: http://www.cplusplus.com/forum/beginner/73054/
It's a simple menu option that uses the arrow keys to select the menu options and Enter to confirm your selection. It currently only works on Windows OS's but I revamped v2 to include C++ functional.

Here is where I got my information on functional: http://www.cplusplus.com/forum/general/73423/

Overall, if you're looking to see how I implemented functional into my program, take a look at: http://pastebin.com/iiCcsYeh
It's a bad example of how the menu works, but it completes the concept of functional.

Specifically in my header file: http://pastebin.com/8XqDGdQh
1
2
3
4
5
6
7
8
9
10
11
12
      // Add new Option with a function
      void AddOption (std::string option, std::function<void ()> func) {
         vMenuOptions.push_back (option);
         vMenuFunctions.push_back (func);
      }
// ...
// And where I call the std::functions at
         case VK_RETURN:
            if(vMenuFunctions[(iLastSelection - 1)]) {
               VP_ClearScreen();
               vMenuFunctions[(iLastSelection - 1)]();
            }


Again, not the best code I've wrote, but it does work. The menu is also set up to stop looping if the user presses enter on an option that doesn't have a function associated with it. It was the only way I could get an Exit option to effectively work.

One day I'll go back to it and revamp it again.
Somehow, I managed to delete my reply 2 times before I sent it! Sorry if it is not so detailed.

1.)
1
2
3
void operation(int x, int y, int (*FuncToCall)(int a, int b)) {
   std::cout << "Answer is: " << FuncToCall(x, y) << "\n\n";
}

I like your program! It provides a good example of switch-case statements (something else I was looking for). However, I would like to know how the void function is used (how the parameters are sent through and spit back out using cout), but if you could explain to me what *FuncToCall means I think that would clear things up a lot.

2.)
1
2
3
4
5
// Function Prototypes
int addition(int a, int b) { return a + b; }
int subtraction(int a, int b) { return a - b; }
int multiplication(int a, int b) { return a * b; }
int division(int a, int b) { return ((b == 0) ? b : a / b); }

What are these prototyping? Side note-- what is this? { return ((b == 0) ? b : a / b); } (in the prototype for division)

3.) I did not manage to understand any of your code (of course) and the forum you pointed me to (no pun intended) was very complex as well (although it looks very familiar). If functionals and function pointers really do the same thing, I'll just stick with function pointers since I've started that first.
1) I'm glad you liked it. But regarding the function operation it accepts three parameters. 1 and 2 are just integers, obviously, while the third is the function pointer itself. It is basically a mini prototype. It says that when calling the function FuncToCall, you need to pass it two parameters, int a and int b.

If you think about it, it's just a new name for another function. But you have to look at what function pointer was passed, so you somewhat have to work backwards.
operation gets called with num1, num2, and mathFunc.
mathFunc get's assigned the function either addition, subtraction, etc.

Looking at the declaration of mathFunc, you see that it's "prototype" if you will says that it accepts two int values, so it is allowed to point at any function that also accepts and two int values. Again, this isn't very effective considering the fact that it's not a very in depth system and I haven't played with the C function pointers at all before this.

But the thing to remember is that a function pointer can be very useful when you want to run a function when you call another function, but have no idea what that function is going to be. Using C function pointers, you're limited to what you can call. However, using C++ functional, you have the option of not only directly calling a function, but you can pass a bound function or a lambda function, both of which require some thinking to accomplish.

I can't really explain it better, but if you're really trying to learn C function pointers, I suggest creating a new thread in the General Programming forum so you can get some better explanations on it.

2) They started out as prototypes, but I got lazy and just decided to define them there. I simply forgot to change my comment.

As for this line: return ((b == 0) ? b : a / b); it's a simple return statement, but it returns the value of a ternary condition. A ternary condition is a basic inline if/else statement. The syntax is:
1
2
3
4
5
6
(condition) ? trueExpression : falseExpression;
// Which can be wrote like
if (condition)
   trueExpression;
else
   falseExpression;


Putting it back into context, it's the same as writing this:
1
2
3
4
if (b == 0)
   return 0;
else
   return a / b;


Sometimes I get carried away with using them, but I figured it was ok just to slide that in to prevent dividing by 0.

3) I figured it was over your head, but it is a good overall reference. It goes into depth over three good C++ features, std::function, std::bind, and lambda, which is what [] means. Each one of those features takes a fairly long time to master, but has it's place in the language. Most of it was an attempt to fill in gaps that the standard was missing, or things that were never converted to C++ from C.

As for function pointers vs functional, I highly suggest functional. It's much easier to understand since it has it's own class type and you can easily pass anything you want to it, especially with the introduction of lambdas.

On a side note, don't get worked up over the ugliness of lambda expressions, they're quite ugly, but albeit effective.
I am making progress (thank you so much) but I have a few more questions.

1.) Is FuncToCall a common C++ expresssion? This is not the first time I've seen it some code. Did you just happen to name it like this, and you could have just called it "dog," for example? Or is this a standard expression that must be worded as such for it to have meaning in the context of the program.

2.) Going back to the example given by the tutorial on this website:
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
// pointer to functions
#include <iostream>
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))
{
  int g;
  g = (*functocall)(x,y);
  return (g);
}

int main ()
{
  int m,n;
  int (*minus)(int,int) = subtraction;

  m = operation (7, 5, addition);
  n = operation (20, m, minus);
  cout <<n;
  return 0;
}

Now, I'm deducing with my new knowledge, this line: m = operation (7, 5, addition); sends the parameters (7, 5, addition) to function operation. Int x is set equal to 7, int y is set equal to 5, and addition becomes functocall. The next line declares int g, and the next line declares g to be equal to the addition of x and y. This line sends the values of x and y to function addition, a becomes equal to 7, y becomes equal to 5, these are added, and sent back to g. The new value of g (12) is returned to m.

Now, in this line: n = operation (20, m, minus); n sends the parameters to function operation, like before, but this time, the third parameter is minus. Minus happens to be a pointer declared near the beginning of main that accepts two parameters and "points" to function subtraction. In this way, the third parameter is indirectly function subtraction. 20 and 12 are fed through function subtraction the same way 7 and 5 were passed through function addition. 20-12=8, therefore g=8, and therefore n=8. Finally, we display 8 with cout.

Correct me if I'm wrong anywhere in there, and please do not forget about question 1. I know you tried to say a lot of the things I mentioned in my possible explanation earlier in this thread, but now I think/hope I'm getting it.

Sorry for delayed responses, I don't want to mention something I will regret asking because I solve it myself 2 minutes later.
1) It was just the name I came up with, and yes, I could have named it dog, but I try to name my variables with a meaning. Also, since it was a function call, I used CapitalCamelCase (I use that for all of my function names).

2) You're absolutely right. As you can see, the logical thinking behind function pointers is a little hard to follow sometimes, and it makes it even harder to read someone else's code unless you understand them. That is exactly why I prefer the concept of std::function better. The function prototype specifically states that you're going to be passing a function to that function. Not only does this come in handy because you can assign a variable to store a function and pass it later on in the program.

The C++ version is much slicker and cleaner looking and compared side by side, I believe the C++ version is much easier to read. If I get some time, I'll rewrite my example calculator using the C++ functional so you can see the difference, side by side, of C and C++.

And like I said, we also have the lambda functions, but they're more of a special case, when needed, feature.
1.) Alright, I just wanted to know if it was a term such as continue that is a built-in C++ expression.

2.) Finally I understand it! Could you please do the side-by-side comparison some day? Take your time though, I think I've asked too much already.

3.) I'll take you're earlier advice and make a thread on function pointers.

Thank you so much!

~Solved~
Here is the std::function version. Since it isn't easy to post code side by side here, I made the output of the program be the code of the first one. The C version doesn't have highlighting but you can see the differences in the lines that are underlined:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <functional>
#include <iostream>

// Function Prototypes
int addition(int a, int b) { return a + b; }
int subtraction(int a, int b) { return a - b; }
int multiplication(int a, int b) { return a * b; }
int division(int a, int b) { return ((b == 0) ? b : a / b); }

// Function that uses the function pointer named FuncToCall
void operation(int x, int y, std::function<int(int,int)> FuncToCall);

int main() {
   // Declare our variables to hold the two numbers
   int num1, num2;
   // Declare our variable to hold the sign
   char operand;
   // Declare our function pointer variable
   std::function<int(int,int)> mathFunc;

   do {
      std::cout << "Please enter your equation (1!1 to quit): ";
      std::cin >> num1 >> operand >> num2;

      switch (operand) {
            // Addition
         case '+':
            // Assign mathFunc to the addition function
            mathFunc = addition;
            break;
            // Subtraction
         case '-':
            // Assign mathFunc to the subtraction function
            mathFunc = subtraction;
            break;
            // Multiplication
         case '*':
            // Assign mathFunc to the multiplication function
            mathFunc = multiplication;
            break;
            // Division
         case '/':
            // Assign mathFunc to the division function
            mathFunc = division;
            break;
         default:
            continue;
      }
      // Call our operation function with the function pointer
      operation(num1, num2, mathFunc);
   } while (operand != '!');

   return 0;
}

void operation(int x, int y, std::function<int(int,int)> FuncToCall) {
   std::cout << "Answer is: " << FuncToCall(x, y) << "\n\n";
}

#include <iostream>

// Function Prototypes
int addition(int a, int b) { return a + b; }
int subtraction(int a, int b) { return a - b; }
int multiplication(int a, int b) { return a * b; }
int division(int a, int b) { return ((b == 0) ? b : a / b); }

// Function that uses the function pointer named FuncToCall
void operation(int x, int y, int (*FuncToCall)(int a, int b));

int main() {
   // Declare our variables to hold the two numbers
   int num1, num2;
   // Declare our variable to hold the sign
   char operand;
   // Declare our function pointer variable
   int (*mathFunc)(int a, int b);

   do {
      std::cout << "Please enter your equation (1!1 to quit): ";
      std::cin >> num1 >> operand >> num2;

      switch (operand) {
            // Addition
         case '+':
            // Assign mathFunc to the addition function
            mathFunc = addition;
            break;
            // Subtraction
         case '-':
            // Assign mathFunc to the subtraction function
            mathFunc = subtraction;
            break;
            // Multiplication
         case '*':
            // Assign mathFunc to the multiplication function
            mathFunc = multiplication;
            break;
            // Division
         case '/':
            // Assign mathFunc to the division function
            mathFunc = division;
            break;
         default:
            continue;
      }
      // Call our operation function with the function pointer
      operation(num1, num2, mathFunc);
   } while (operand != '!');

   return 0;
}

void operation(int x, int y, int (*FuncToCall)(int a, int b)) {
   std::cout << "Answer is: " << FuncToCall(x, y) << "\n\n";
}


I figured I'd explain the syntax of std::function as well. The basic syntax is this:
std::function<returnDataType(param1,param2,...)> functionPointerName;

As you can see, visually looking at it, it's not much different, but there is no question as to what you're looking at. std::function clearly specifies what it is. In the example above, I didn't need to use std::function, but simply function, because I defined the std namespace at the beginning. I wanted to show you what the actual name of it is, just like std::cout and std::cin.

I'm glad you understand the concept. It's about putting it to use in a program that makes it hard.

Also, I noticed your new thread, I hope someone comes along and helps you.
Topic archived. No new replies allowed.