How to Print Member Function Address in C++

How to print member function address in C++

I don't believe that there are any facilities provided by the language for doing this. There are overloads for operator << for streams to print out normal void* pointers, but member function pointers are not convertible to void*s. This is all implementation-specific, but typically member function pointers are implemented as a pair of values - a flag indicating whether or not the member function is virtual, and some extra data. If the function is a non-virtual function, that extra information is typically the actual member function's address. If the function is a virtual function, that extra information probably contains data about how to index into the virtual function table to find the function to call given the receiver object.

In general, I think this means that it's impossible to print out the addresses of member functions without invoking undefined behavior. You'd probably have to use some compiler-specific trick to achieve this effect.

Hope this helps!

How to print function pointers with cout?

There actually is an overload of the << operator that looks something like:

ostream & operator <<( ostream &, const void * );

which does what you expect - outputs in hex. There can be no such standard library overload for function pointers, because there are infinite number of types of them. So the pointer gets converted to another type, which in this case seems to be a bool - I can't offhand remember the rules for this.

Edit: The C++ Standard specifies:

4.12 Boolean conversions

1 An rvalue of arithmetic,
enumeration, pointer, or pointer to
member type can be converted to an
rvalue of type bool.

This is the only conversion specified for function pointers.

Class member function address

Member function pointers are not per object, they are per type. In your example, you could have multiple instances of a IDirect3DDevice9, all of which would have the same pointer value for their EndScene member function (assuming they aren't different concrete types - but this isn't likely).

The specific error you are getting is because you are attempting to get the address of a pointer-to-member function from an object, which isn't valid (eg. see '&' illegal operation on bound member function expression error).

It is possible to get the value of a member function using the type, instead of an object pointer. However, it's extremely ugly:

// Value is stored in 'end_scene':
HRESULT (IDirect3DDevice9::* end_scene)() = &IDirect3DDevice9::EndScene;
// Call the function, with the value of 'end_scene'.
(*d3ddev.*(end_scene))();
// Print the address of the pointer-to-member function:
printf("%p\n", end_scene);

I wouldn't suggest doing this here, because most functions in IDirect3DDevice9 don't have the same prototype. In fact, only BeginScene has the same prototype as EndScene, and it's hard to imagine a situation in which the call could be one or the other, since they need to be called in a specific order. You could make the case about using this for the functions that get/set vertex/pixel shader constants, as they have the same prototypes, but, it's just as easy to store some other external state to determine which function to call, and much more straightforward.

What's the easiest way to print the address of a member function?

[Edited] As far as I know, there's no way to get the address of the function that actually will be called, regardless of virtualness. For virtual methods specifically, the method and destination of virtual dispatch is left to the implementation. However even for normal functions the function pointer may not be the actual code address of the function (although I suspect there are cases where it is).

Getting address of class member function and calling it from pointer

The function call operator () has higher precedence than the dereference operator *. That means you need parenthesis to call a member-function pointer:

(this->*statScreenPointer)();

How do I print the address of a method pointer in C++?

Member function pointer is generally an object with non-trivial internal structure. Which is why you can't print it using tools intended for printing primitive types. Casting the pointer to void * is not a viable approach, since the size of member pointer is generally larger than sizeof(void *). Forcefully casting it to void * will in any case discard a portion of the pointer representation, thus no longer guaranteeing the uniqueness.

If what you are looking for is a unique string generated from the pointer, you can reinterpret the pointer as a character array, and use the string representation of character values in the identifier. Something like

void (A::*p)(void) = &A::m;

for (size_t i = 0; i < sizeof p; ++i)
printf("%d ", reinterpret_cast<char *>(&p)[i]);

printf("\n");


Related Topics



Leave a reply



Submit