N
Nimmi Srivastav
When I was learning C, I learned that the data type of a function name
by itself is a function pointer. For example
int
someFunction(char* str)
{
....
}
....
int (*fPtr) (char *) = someFunction;
....
I was, therefore, extremely surprised (while reading about pointers to
non-static member functions) when I saw that in C++, you need to
supply the address operator in front of the function name. That is,
the above statement will be something like this:
int (*fPtr) (char *) = &someFunction;
To explain what I am saying, let me take the liberty to include a
small code snippet from "C++ FAQs Second Edition" by Marshall Cline et
al.
FAQ 38.04 (How is an array of pointers to non static member functions
declared?)
class Fred {
public:
void f(int i);
void g(int i);
void h(int i);
};
....
typedef void (Fred::*FredMemberPtr)(int);
....
FredMemberPtr array[3] = {&Fred::f, &Fred::g, &Fred::h};
....
According to me, the above statement should have been something like
this:
FredMemberPtr array[3] = {Fred::f, Fred::g, Fred::h};
Why is the address operator (&) needed in this case at all? Kindly
explain. Is there a difference between C and C++ in this respect?
Thanks,
Nimmi
by itself is a function pointer. For example
int
someFunction(char* str)
{
....
}
....
int (*fPtr) (char *) = someFunction;
....
I was, therefore, extremely surprised (while reading about pointers to
non-static member functions) when I saw that in C++, you need to
supply the address operator in front of the function name. That is,
the above statement will be something like this:
int (*fPtr) (char *) = &someFunction;
To explain what I am saying, let me take the liberty to include a
small code snippet from "C++ FAQs Second Edition" by Marshall Cline et
al.
FAQ 38.04 (How is an array of pointers to non static member functions
declared?)
class Fred {
public:
void f(int i);
void g(int i);
void h(int i);
};
....
typedef void (Fred::*FredMemberPtr)(int);
....
FredMemberPtr array[3] = {&Fred::f, &Fred::g, &Fred::h};
....
According to me, the above statement should have been something like
this:
FredMemberPtr array[3] = {Fred::f, Fred::g, Fred::h};
Why is the address operator (&) needed in this case at all? Kindly
explain. Is there a difference between C and C++ in this respect?
Thanks,
Nimmi