|
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Thu May 18, 2017 11:57 am Post subject: (C++) Pointers to function? |
|
|
I got this example from an online tutorial, which talks about pointers to functions.
Code: |
// 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)) <----------line-1
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
int (*minus)(int,int) = subtraction;
m = operation (7, 5, addition); <------------------- line-2
n = operation (20, m, minus);<--------line-3
cout <<n;
return 0;
}
The result is 8.
|
I understand that "minus" is a pointer, which points to the function "subtraction".
The part I do not understand is that the third parameter in "operation" should be a pointer to a function, as we can see from its definition (line-1). However, "addition" is not a pointer, not like "minus", "addition" is just a ...function. So, why is "line-2" OK? Are functions inherently pointers?
Thanks a lot.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
ParkourPenguin I post too much Reputation: 140
Joined: 06 Jul 2014 Posts: 4289
|
Posted: Thu May 18, 2017 12:18 pm Post subject: |
|
|
No, functions are not pointers, but any lvalue of function type can be implicitly converted to an rvalue pointer to that function (as shown in your example).
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
|
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Thu May 18, 2017 1:03 pm Post subject: |
|
|
ParkourPenguin wrote: | No, functions are not pointers, but any lvalue of function type can be implicitly converted to an rvalue pointer to that function (as shown in your example). |
Thanks, Penguin. Would you mind telling me where you found this info? I would like to learn more about it.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
ParkourPenguin I post too much Reputation: 140
Joined: 06 Jul 2014 Posts: 4289
|
Posted: Thu May 18, 2017 1:34 pm Post subject: |
|
|
I pulled that from my head, but it's defined in §4.3 of the standard:
Quote: | An lvalue of function type T can be converted to a prvalue of type “pointer to T.” The result is a pointer to the function. |
If you're learning C++, I'd recommend C++ Primer (5th Edition). It's a decent comprehensive overview of C++11. Of course, some stuff has changed since then, but it's a lot better than the vast majority of tutorials you'll find online.
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
|
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Thu May 18, 2017 5:00 pm Post subject: |
|
|
ParkourPenguin wrote: | I pulled that from my head, but it's defined in §4.3 of the standard:
Quote: | An lvalue of function type T can be converted to a prvalue of type “pointer to T.” The result is a pointer to the function. |
If you're learning C++, I'd recommend C++ Primer (5th Edition). It's a decent comprehensive overview of C++11. Of course, some stuff has changed since then, but it's a lot better than the vast majority of tutorials you'll find online. |
Thanks for the tip, Penguin.
I have C++ Primer(5th) and Stroutstrup's Programming Principles and Practice Using C++(2nd). I read both of them, but they are both thick books, so I think I will take your advice and stick to C++ Primer.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|