Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


(C++) Pointers to function?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Dr.Disrespect
Master Cheater
Reputation: 1

Joined: 17 Feb 2016
Posts: 473

PostPosted: Thu May 18, 2017 11:57 am    Post subject: (C++) Pointers to function? Reply with quote

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. Very Happy
Back to top
View user's profile Send private message
ParkourPenguin
Grandmaster Cheater Supreme
Reputation: 49

Joined: 06 Jul 2014
Posts: 1694
Location: Arcadian Suburbia

PostPosted: Thu May 18, 2017 12:18 pm    Post subject: Reply with quote

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
View user's profile Send private message
Dr.Disrespect
Master Cheater
Reputation: 1

Joined: 17 Feb 2016
Posts: 473

PostPosted: Thu May 18, 2017 1:03 pm    Post subject: Reply with quote

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. Smile

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
ParkourPenguin
Grandmaster Cheater Supreme
Reputation: 49

Joined: 06 Jul 2014
Posts: 1694
Location: Arcadian Suburbia

PostPosted: Thu May 18, 2017 1:34 pm    Post subject: Reply with quote

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
View user's profile Send private message
Dr.Disrespect
Master Cheater
Reputation: 1

Joined: 17 Feb 2016
Posts: 473

PostPosted: Thu May 18, 2017 5:00 pm    Post subject: Reply with quote

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. Smile

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites