Is there a way to tell if a static method of a class was invoked by a call to the class method or by a call from an object?
class mertz
{
static int x(void) {....}
....
}
int main( ... )
{
int c = mertz::x();
mertz fred;
ind d = fred.x();
}
Can x tell the difference in its invocation in for c and d?
Thanks.
Not in a portable way and probably not at all in most C++ implementations.
A static method is indeed just a global function with a funny name. You can in theory inspect the call stack to find from where the call is coming from… however the machine instructions that will be generated by the compiler most probably will be identical for both
and
because only the static type of the instance is needed to find which is the method to call and this information is available at compile time. Therefore in both cases the call code will just call the function without any need to reference the
fredinstance.