Dev C++ Send Integer To A Function

  1. Dev C++ Send Integer To A Function Number

We've previously talked about pointers to data structures and class objects, however in C++ we can also have pointers to functions. These pointers behave a little bit differently that the other pointers we've come across so far, but by the end of this tutorial you should know how to use function pointers, as well as knowing the basics of what you can and cannot do with them.

The most confusing thing about function pointers is probably their syntax - you write the pointers, as follows:

  1. In the last program C program to add two integer numbers, we discussed how to take input and find the sum of two integer numbers? In this program we are doing the same but using a user defined function, this program will take two integer numbers are calculate the sum/addition of them using a user defined function.
  2. Jun 18, 2012  Sounds like I can really use this. I have a few more questions since I'm on my phone and can't actually try it, but let's say I want to call myMenu.AddOption('Settings', settingsMenu.Play(1)).

The copy-initialization of the result of the function call is sequenced-before the destruction of all temporaries at the end of expression, which, in turn, is sequenced-before the destruction of local variables of the block enclosing the return statement. (since C14).

The above would work as expected, outputting 'One' and then 'Two'. When setting and accessing the value of function pointers, note that the dereference and 'address of' operators aren't actually necessary - usually they aren't used for the sake of simplicity, as shown in the modified snippet below:

If a function pointer needs to point to functions that take parameters, the pointers, by specifying a number of elements in square brackets next to the pointer name. If we wanted a function pointer array which contained pointers to both functions 'one' and 'two' in elements of index 0 and 1, we could use the following:

The above would work exactly as our previous one did, apart from both functions will be accessible any time by using 'fptr[0]' and 'fptr[1]'. As you can probably tell, practical yet simple examples of function pointers aren't particularly easy to come up with - generally function pointers become useful due to the fact that we can indirectly call a function without knowing at compile-time which function we're going to call. Perhaps 'fptr[0]' in our example may change to a pointer to a third function, 'three', in some conditional situation, in which case 'fptr[0]' will yield different results in different circumstances. We'll cover a practical example with a basis like this in a minute. Before we move on to a 'different type' of function pointers and create this more practical example though, be aware that pointer arithmetic doesn't work on function pointers.

We can also create member function pointers in C++! The syntax is essentially the same as function pointers, however the class name followed by the scope resolution operator prepends the pointer name - so data-type (ClassName::*pointerName)(parameters);. The syntax is messy, but it works. With member functions, the 'address of' and dereference operators are necessary during assignment and access - because of this, calling member functions via member function pointers on class objects usually requires a syntax that looks like object.*pointerName to dereference the pointer before performing the member function on the object. This is all, once again, quite difficult to explain in words - so a simple code example may be of help:

The above should work as expected - the difference between our previous examples clearly being that this pointer could be used to indirectly call a member function upon any 'Number' object. Like 'regular' function pointers, you can also create arrays of member function pointers - so the following would perform the same as the snippet above, but would leave 'NumberPtr[0]' and 'NumberPtr[1]' available for calling any time:

With member function pointers covered, at least conceptually, I once again recommend you take a quick break from this tutorial to try this for yourself. It's important that you get used to the syntax and also understand what function pointers (of this kind) are capable of.

So to tie off this tutorial, we're going to create a quick example with a practical use of a member function pointer. The plan is to create a basic class named 'Employee' which will hold some basic information about any given employee of a company. Most importantly, this class will contain a 'Pay' member function which will call a private function via a function pointer to get the salary of the employee (based on the number of hours they've worked). The member function pointer will actually be a member of the 'Employee' class, and it comes in handy when we decide that employees are either normal or experienced -- the member function pointer for experienced users will point towards a different private member function which will calculate a higher wage for experienced employees. Again, this isn't all that easy to explain, but from reading this (and previous) tutorial(s), you should (hopefully) understand the commented code that follows:

The above should show '100' and then '200', as the employee, 'one', is first calculated to be paid as a 'regular' employee, and then as an experienced employee.

Dev

Dev C++ Send Integer To A Function Number

On a side note, basically the reason I've been wanting to do this is so that when you select an option, I want the menu class to handle everything, as I've said before. I'm thinking I'm going to make an option class that has a name and a function, but I'm thinking about other ideas that can be implemented, such as on/off switches, values 1-10 that the user can increase or decrease, and whatever else that has been seen in option menus.
My other question is, what do you think is the best way to implement other options other than just functions? They seem to be easy.
I was thinking for like the on/off toggle, to allow the user to hit enter and turn the option on/off, but I'm not sure how to go about this. That's why I asked about the template. AddOption could accept (string, function) (string, bool) (string, int) but I am not sure if the template covers functions or not. That's why I asked earlier about them. I've also never used a template before, but am very interested in them, they seem extremely handy when having to write once and only once.