Explanation of function pointers

0 votes

I'm having trouble comprehending some C++ syntax when paired with function pointers and function declarations, specifically:

When we wish to declare a kind of function, we usually write something like:

I'm happy with typedef void(*functionPtr)(int); 

FunctionPtr is now a type that represents a pointer to a function that returns void and takes an int as an argument.

We may put it to use as follows:

typedef void(*functionPtr)(int);

void function(int a){
    std::cout << a << std::endl;
}

int main() {

    functionPtr fun = function;
    fun(5);

    return 0;
}

And 5 are printed on a screen.

We have a pointer to function fun, which we assign to function - function, and we run this function using a pointer. 

Cool.
Now, according to some books, function and pointer to function are treated similarly, so after declaring the function() function, whenever we say function, we mean both real function and pointer to function of the same type, so the following compiles and every instruction yields the same result (5 printed on a screen):

int main() {

    functionPtr fun = function;
    fun(5);
    (*fun)(5);
    (*function)(5);
    function(5);

    return 0;
}

So, as long as I can conceive that pointers to functions and functions are essentially the same, it's great with me.

Then I thought, since the pointer to function and the real function are the same, why can't I perform the following:

typedef void(functionPtr)(int); //removed *

void function(int a){
    std::cout << a << std::endl;
}

int main() {

    functionPtr fun = function;
    fun(5);

    return 0;
}

This gives me following error:

prog.cpp:12:14: warning: declaration of 'void fun(int)' has 'extern' and is initialized functionPtr fun = function;

Jun 20 in C++ by Nicholas
• 2,520 points
8 views

1 answer to this question.

0 votes

It's a little perplexing.

Function type and pointer to function type are distinct kinds (no more similar than int and pointer to int). 

However, in virtually all cases, a function type decays to a reference to a function type. 

In this context, rotting roughly refers to conversion (there is a difference between type conversion and decaying, but you are probably not interested in it right now).

What matters is that practically every time you use a function type, you end up with a reference to the function type. 

But take note of the nearly - almost every time is not always!

And there are rare circumstances where it does not.

typedef void(functionPtr)(int);
functionPtr fun = function;

This code tries to clone one function to another (not the pointer! the function!) 

However, this is not feasible since functions in C++ cannot be copied. 

The compiler does not let this, and I'm surprised you got it compiled (you say you got linker errors?)

Now for the code:

typedef void(functionPtr)(int);
functionPtr function;
function(5);

function does not shadow anything. Compiler knows it is not a function pointer which can be called, and just calls your original function.

answered Jun 21 by Damon
• 4,240 points

Related Questions In C++

0 votes
1 answer

Sorting a vector of custom objects

A simple example using std::sort struct MyStruct { ...READ MORE

answered Jun 1 in C++ by Damon
• 4,240 points
27 views
0 votes
0 answers

Intersection of two std::unordered_map

I have two std::unordered_map std::unordered_map<int, int> mp1; std::unordered_map<int, int> ...READ MORE

May 31 in C++ by Nicholas
• 2,520 points
34 views
0 votes
0 answers

What is the C++ function to raise a number to a power?

What's the best way to raise a n ...READ MORE

Jun 1 in C++ by Nicholas
• 2,520 points
17 views
0 votes
1 answer

std::greater on a an std::pair of a double and a class

std::greater is simply a wrapper for a ...READ MORE

answered Jun 2 in C++ by Damon
• 4,240 points
17 views
0 votes
1 answer

What is a smart pointer and when should I use one?

A smart pointer is similar to a ...READ MORE

answered Jun 2 in C++ by Damon
• 4,240 points
10 views
0 votes
1 answer

Using :: (scope resolution operator) in C++

You're mostly correct regarding cout and cin. ...READ MORE

answered 3 days ago in C++ by Damon
• 4,240 points
10 views
0 votes
1 answer

In C++ abs( *a - *b) does not return absolute value of negative number

On the first line, you reallocated *a, and it is now utilising that new value on the second line.  int origa = *a; *a = abs(origa + ...READ MORE

answered 3 days ago in C++ by Damon
• 4,240 points
7 views
0 votes
0 answers

C++ pointer to objects

Is it always necessary in C++ to  ...READ MORE

3 days ago in C++ by Nicholas
• 2,520 points
4 views
0 votes
1 answer

Syntax of priority queue

We must first include the queue header file in order to establish a priority queue in C++. #include <queue> Once we import this file, we ...READ MORE

answered May 31 in C++ by Damon
• 4,240 points
11 views
0 votes
1 answer

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

answered Jun 1 in C++ by Damon
• 4,240 points
36 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP