Pointers to function question
Why both of the next examples are the same?
Ex1 : int*(*foo)() = Function;
Ex2: int*(*foo)() = &Function;
Notice: In the second example i'm using & operator.
That means that c++ takes function name as an address of it and this () dereferencing it just like in arrays?
laconstantine
Junior Poster in Training
70 posts since May 2007
Reputation Points: 10
Solved Threads: 1
Yes, the use of & is optional in this case. Similarly, when calling the function *foo() and foo() will also be the same.
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
...c++ takes function name as an address of it...
c++ takes the name of the function (as an expression) to be either a reference to the function or a pointer to the function. eg.
#include <iostream>
void foo( void(*pfn)() ) { std::cout << "pointer\n" ; }
void foo( void(&rfn)() ) { std::cout << "reference\n" ; }
void bar() { std::cout << "bar\n" ; }
int main()
{
bar() ; // ok
(*bar)() ; // ok , bar == &bar, so (*bar) == bar
(**bar)() ; // also ok
(***bar)() ; // also ok
foo(bar) ; // error - ambiguous is bar a reference or a pointer?
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
Why this is legal?
(****bar)() ;
and here is not?
int x =2;
int *ptr = &x;
cout << (***ptr);
???
laconstantine
Junior Poster in Training
70 posts since May 2007
Reputation Points: 10
Solved Threads: 1
Why this is legal?
(****bar)() ;
void bar() { /* ... */ }
int main()
{
void (*pfn)() = bar ; // ok, bar is treated as &bar
void (&rfn)() = *pfn ; // ok, *pfn is a reference to the function
pfn = rfn ; // ok, equivalent to pfn = &rfn ;
pfn = *bar ; // ok, equivalent to pfn = &*&bar ;
pfn = **bar ; // ok, equivalent to pfn = &*&*&bar ;
pfn = ***bar ; // ok, equivalent to pfn = &*&*&*&bar ;
(*pfn)() ; // ok, call bar
(****bar)() ; // ok, equivalent to (*pfn)() ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
So its
(***FuncName)()
Its legal because functions have no values so the dereferencing asterik has no effect on the function name?
laconstantine
Junior Poster in Training
70 posts since May 2007
Reputation Points: 10
Solved Threads: 1
the dereference operator (*) on a pointer gives a reference to what is pointed to. the address of operator on a reference gives a pointer to what is being referred to. since a reference to a function can be implicitly treated as a pointer (an implicit address of is assumed),
*f == *(&f) == f
it really has got nothing to do with values or data. a void* points to some data (we do not know what it's type is), but cannot be dereferenced. an int* points to an int, so dereferencing it gives us a reference to an int (int&).
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
what is wrong here?
void foo()
{
cout << "Foo Called";
}
int main()
{
(****foo)();
return 0;
}What is wrong?
laconstantine
Junior Poster in Training
70 posts since May 2007
Reputation Points: 10
Solved Threads: 1
Ah sorry i thought in your post 10 you wrote that something is wrong here i just miss understood, anyway thanks:)
laconstantine
Junior Poster in Training
70 posts since May 2007
Reputation Points: 10
Solved Threads: 1