Reputation: 9061
I have a simple question:
What is the type of function in C or C++
As we can have pointers to function in C or C++, that means functions should have a specific type otherwise type checking during pointers to function creation have no meaning.
Can someone explain me, I am on the correct path or not?
If I am on the right path, How can I find the type of function?
Upvotes: 4
Views: 17456
Reputation: 58271
The type of a function in C/C++ includes both the return type
and the types of input parameters
.
Consider the following function declaration:
int function(char, float);
A pointer to that function has the following type:
int (*funptr)(char, float);
Similarly in general :
returntype function (argtype1, argtype2, argtype3)
A corresponding pointer to such a function is
returntype (*ptr) (atgtype1, atgtype2, atgtype3);
There are be many different types of functions. Find a useful reference on function pointers here.
Also, this classification is based on the return type and argument types
. Functions can also be classified on the basis of scope of their accessibility. like global functions, static functions etc. See here for a short introduction.
Upvotes: 12
Reputation: 12547
Of course every function has it types,
for example, function
double foo(bar& f, const const baz*)
has a type of
function, that accepts reference to bar and constant pointer to baz and return double
It can be written like
double ()(bar&, const baz*)
A pointer to variable of types of that function will have type (variable that can store pointer to that function)
will have type
double (*)(bar&, const baz*)
Or, if you want to typedef
a pointer to functions of that type you can write
typedef double (*func_ptr)(bar&, const baz*)
Again,
func_ptr is a type of pointer to function, that accepts reference to bar and constant pointer to baz and return double
One thing here is that function decays to pointer to function, so you can write
func_ptr f = &foo;
and
func_ptr g = foo;
And it would be the same.
Now imagine, that you have
struct A
{
double goo(bar& f, const const baz*);
};
Now goo
has a type of
function of struct A, that accepts reference to bar and constant pointer to baz and return double
A pointer to this function will have type
double (A::*)(bar&, const baz*)
Note, that it types differs from type of free function foo
. They are not compatible at all.
However, if goo
were static
function, the fact that it belongs to struct A
would be insufficient (as far as member function requires implicit this
argument and static
function does not).
Upvotes: 8
Reputation: 9204
It's actually function signature
which should match either with declaration
or with function pointer
Function signature contains everything as such arguments type , no of arguments and return type.
Directly like variables you cannot say that particular function is of int
type or float
or char
type or so on
Always remember it's signature
as i said above.
Upvotes: 2
Reputation: 1207
Every pair of two types (A,B) has a specific function type A->B. If we choose A=int, B=float, then the function type would become:
float my_function(int a);
Upvotes: -2