meow00
asked on
pointer to a static function
To C++ experts,
If I have a class, and a function like :
class X{} ;
static X* fun(int*) ;
How do I declare a pointer to a function which can point to fun ?
thanks.
meow.
If I have a class, and a function like :
class X{} ;
static X* fun(int*) ;
How do I declare a pointer to a function which can point to fun ?
thanks.
meow.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Exactly if there was no 'static' keyword - this only means the function is accessible only from this file. Even when you declare static function inside the class, the syntax would be the same.
X* (* tFunnc)(int*);
tFunc myFunc = fun;
X* (* tFunnc)(int*);
tFunc myFunc = fun;
ASKER
Hi,
Could anyone please tell me that why not use
"(static X*)(*pMyFunction)(int*);" ? why I don't need static in the very beginning ? Thanks !
meow.
Could anyone please tell me that why not use
"(static X*)(*pMyFunction)(int*);" ? why I don't need static in the very beginning ? Thanks !
meow.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try the following code snippet
int f () {
}
static int f () {
}
This would give u a compile error saying "redefinition of `int f()' "
This demonstrates that static is not at all related to the function signature
Now, technically, static is a type of storage class, thus it only decides what storage class the function belongs to and the behaviour of the compiler gets affected in the corresponding way
When u are declaring a pointer to a function, u just need the function signature and not the storage class of the function
That's the reason for your code to work
HTH
Amit
int f () {
}
static int f () {
}
This would give u a compile error saying "redefinition of `int f()' "
This demonstrates that static is not at all related to the function signature
Now, technically, static is a type of storage class, thus it only decides what storage class the function belongs to and the behaviour of the compiler gets affected in the corresponding way
When u are declaring a pointer to a function, u just need the function signature and not the storage class of the function
That's the reason for your code to work
HTH
Amit
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:
Split: rstaveley {http:#9737786} & migoEX {http:#9741331}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer
I will leave the following recommendation for this question in the Cleanup topic area:
Split: rstaveley {http:#9737786} & migoEX {http:#9741331}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer
X* (*pMyFunction)(int*);
pMyFunction = fun;
pMyFunction(&nData);