Reading the C++ Standard, i see that there are "function" types and "pointer to function" types:
typedef int func(int); // function
typedef int (*pfunc)(int); // pointer to function
typedef func* pfunc; // same as above
I have never seen the function types used outside of examples (or maybe i didn't recognize their usage?). Some examples:
func increase, decrease; // declares two functions
int increase(int), decrease(int); // same as above
int increase(int x) {return x + 1;} // cannot use the typedef when defining functions
int decrease(int x) {return x - 1;} // cannot use the typedef when defining functions
struct mystruct
{
func add, subtract, multiply; // declares three member functions
int member;
};
int mystruct::add(int x) {return x + member;} // cannot use the typedef
int mystruct::subtract(int x) {return x - member;}
int main()
{
func k; // the syntax is correct but the variable k is useless!
mystruct myobject;
myobject.member = 4;
cout << increase(5) << ' ' << decrease(5) << '\n'; // outputs 6 and 4
cout << myobject.add(5) << ' ' << myobject.subtract(5) << '\n'; // 9 and 1
}
Seeing that the function types support syntax that doesn't appear in C (declaring member functions), i guess they are not just a part of C baggage that C++ has to support for backward compatibility.
So is there any use for function types, other than demonstrating some funky syntax?
A friend function does not have 'this' pointer associated with it.
The type of a pointer to a function is based on both the return type and parameter types of the function. In the first declaration, f is interpreted as a function that takes an int as argument, and returns a pointer to an int .
1) Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code. 2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers. 3) A function's name can also be used to get functions' address.
As I understand function pointer is a pointer variable that stores address of a function however pointer to a function is a function which takes function pointer as an argument.
On the one hand, you seem to be talking about function types in general. On the other hand, it appears that your question is really about the usability of typedef names for function types.
The function types themselves is a fundamental concept built into both C and C++ languages. They are used all the time. You just can't live without them. Every time you declare a function you declare an entity of function type. Every time you use a pointer to a function, you use a pointer to function type. Etc.
As for the possibility of making typedef names for function types... I don't see much use for it. It is, I think, a part of C baggage, that was extended a bit for C++ just because it was easy to do.
As you correctly noted typedef names for function types can be used to declare member functions (and in C++ you can also include const
specifier into typedef, as in typedef int MemberType() const;
), but, for example, you can't use this feature with function types passed as template parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With