Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Function pointer
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Alternate C and C++ syntax == The C and C++ syntax given above is the canonical one used in all the textbooks - but it's difficult to read and explain. Even the above <code>typedef</code> examples use this syntax. However, every C and C++ compiler supports a more clear and concise mechanism to declare function pointers: use <code>typedef</code>, but ''don't'' store the pointer as part of the definition. Note that the only way this kind of <code>typedef</code> can actually be used is with a pointer - but that highlights the pointer-ness of it. === C and C++ === <syntaxhighlight lang="C"> // This declares 'F', a function that accepts a 'char' and returns an 'int'. Definition is elsewhere. int F(char c); // This defines 'Fn', a type of function that accepts a 'char' and returns an 'int'. typedef int Fn(char c); // This defines 'fn', a variable of type pointer-to-'Fn', and assigns the address of 'F' to it. Fn *fn = &F; // Note '&' not required - but it highlights what is being done. // This calls 'F' using 'fn', assigning the result to the variable 'a' int a = fn('A'); // This defines 'Call', a function that accepts a pointer-to-'Fn', calls it, and returns the result int Call(Fn *fn, char c) { return fn(c); } // Call(fn, c) // This calls function 'Call', passing in 'F' and assigning the result to 'call' int call = Call(&F, 'A'); // Again, '&' is not required // LEGACY: Note that to maintain existing code bases, the above definition style can still be used first; // then the original type can be defined in terms of it using the new style. // This defines 'PFn', a type of pointer-to-type-Fn. typedef Fn *PFn; // 'PFn' can be used wherever 'Fn *' can PFn pfn = F; int CallP(PFn fn, char c); </syntaxhighlight> === C++ === These examples use the above definitions. In particular, note that the above definition for <code>Fn</code> can be used in pointer-to-member-function definitions: <syntaxhighlight lang="CPP"> // This defines 'C', a class with similar static and member functions, // and then creates an instance called 'c' class C { public: static int Static(char c); int Member(char c); } c; // C // This defines 'p', a pointer to 'C' and assigns the address of 'c' to it C *p = &c; // This assigns a pointer-to-'Static' to 'fn'. // Since there is no 'this', 'Fn' is the correct type; and 'fn' can be used as above. fn = &C::Static; // This defines 'm', a pointer-to-member-of-'C' with type 'Fn', // and assigns the address of 'C::Member' to it. // You can read it right-to-left like all pointers: // "'m' is a pointer to member of class 'C' of type 'Fn'" Fn C::*m = &C::Member; // This uses 'm' to call 'Member' in 'c', assigning the result to 'cA' int cA = (c.*m)('A'); // This uses 'm' to call 'Member' in 'p', assigning the result to 'pA' int pA = (p->*m)('A'); // This defines 'Ref', a function that accepts a reference-to-'C', // a pointer-to-member-of-'C' of type 'Fn', and a 'char', // calls the function and returns the result int Ref(C &r, Fn C::*m, char c) { return (r.*m)(c); } // Ref(r, m, c) // This defines 'Ptr', a function that accepts a pointer-to-'C', // a pointer-to-member-of-'C' of type 'Fn', and a 'char', // calls the function and returns the result int Ptr(C *p, Fn C::*m, char c) { return (p->*m)(c); } // Ptr(p, m, c) // LEGACY: Note that to maintain existing code bases, the above definition style can still be used first; // then the original type can be defined in terms of it using the new style. // This defines 'FnC', a type of pointer-to-member-of-class-'C' of type 'Fn' typedef Fn C::*FnC; // 'FnC' can be used wherever 'Fn C::*' can FnC fnC = &C::Member; int RefP(C &p, FnC m, char c); </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)