Find The Address Of An Overloaded Function In C++
You
can obtain the address of a function. One reason to do so is to assign the
address of the function to a pointer and then call the function through that
pointer. If the function not overloaded this process is straightforward, for
overloaded function the process requires a little more subtlety. To
understanding the first consider this statement which assigns the address of
some function called myfunc() to
pointer called p:
p=myfunc;
If
myfunc() is not overloaded there is
one and only one function called myfunc(),and
the compiler has no difficulty assigning its address to p.
Fig: Find The Address Of An Overloaded Function In C++
Program:
#include<iostream.h>
class
BCA;
int
myfunc(int a);
int
func(int a, int b);
int
main()
{
int
(*fp) (int a);
fp=myfunc;
cout<<”fp(5);
return
0;
}
int
myfunc(int a)
{
return
a;
}
int
myfunc(int a,int b)
{
return
a*b;
}
Here
, there are two type version of myfunc().
Both return int, but take a single integer argument; the other requires two
integer aruguments. In the program, fp
is declared as a pointer to a function the return an integer and that take one
integer argument. When fp is
assigned the address of myfunc(),
c++ uses this information to select that myfunc(int
a) version of myfunc(). Had fp been declared like this:
int (*fp) (int a, int b);
No comments:
Post a Comment