| | |
Pointers to function question
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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.
C++ Syntax (Toggle Plain Text)
#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? }
You're getting confused between two very distinct topics in C/C++ - Pointers, and function pointers, which are both a rather different kettle of fish.
The reason that function pointers can be handled in such a way as shown in your original post is because of the nature of functions themselves.
in C/C++, you can really only do two things with a function - you may call it, or you may take its address.
Contrast this with a variable, you may also take its address, but you may not call it. In addition to this, unlike functions, you can assign to it, and you can obtain the data it stores.
Since functions and variables are so different, it follows that a pointer-to-function behaves differently to a pointer-to-variable.
Just to put the above into context, when you use the name (identifier) of a function, without the trailing parentheses, the compiler already knows that the function cannot have a value stored, therefore the address of the function is returned instead - the & (address-of) operator is unnecessary.
When you use the name/identifier of a variable, the compiler assumes you wish to obtain its stored value. Hence, if you wish to obtain the address of a variable you must explicitly use the '&' (address-of) operator.
The example you posted using the * (dereference) operator is not legal for the same reasons listed above. this is - a function has no stored data value, so explicitly dereferencing a pointer-to-function does nothing.
On the other hand A variable does store a value, so explicitly de-referencing a pointer-to-variable does have an effect (the effect is to return the variable itself). Ordinary non-pointer variables may not be de-referenced (This would be a nonsensical operation)
The reason that function pointers can be handled in such a way as shown in your original post is because of the nature of functions themselves.
in C/C++, you can really only do two things with a function - you may call it, or you may take its address.
Contrast this with a variable, you may also take its address, but you may not call it. In addition to this, unlike functions, you can assign to it, and you can obtain the data it stores.
Since functions and variables are so different, it follows that a pointer-to-function behaves differently to a pointer-to-variable.
Just to put the above into context, when you use the name (identifier) of a function, without the trailing parentheses, the compiler already knows that the function cannot have a value stored, therefore the address of the function is returned instead - the & (address-of) operator is unnecessary.
When you use the name/identifier of a variable, the compiler assumes you wish to obtain its stored value. Hence, if you wish to obtain the address of a variable you must explicitly use the '&' (address-of) operator.
The example you posted using the * (dereference) operator is not legal for the same reasons listed above. this is - a function has no stored data value, so explicitly dereferencing a pointer-to-function does nothing.
On the other hand A variable does store a value, so explicitly de-referencing a pointer-to-variable does have an effect (the effect is to return the variable itself). Ordinary non-pointer variables may not be de-referenced (This would be a nonsensical operation)
Last edited by Bench; May 23rd, 2007 at 9:07 am.
¿umop apisdn upside down? •
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
C++ Syntax (Toggle Plain Text)
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)() ; }
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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&).
*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&).
•
•
•
•
So its
(***FuncName)()
Its legal because functions have no values so the dereferencing asterik has no effect on the function name?
although it would be noteworthy to say that code like this doesn't pass QA when it comes to readability.
CPP Syntax (Toggle Plain Text)
void foo() { } int main() { (****foo)(); }
Last edited by Bench; May 24th, 2007 at 7:18 am.
¿umop apisdn upside down? ![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: C++ A little homework help, Thanks very much for the last assistance
- Next Thread: C++ homework help on files
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






