Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print address of virtual member function

I am trying to print the address of a virtual member function. If I know which class implements the function I can write:

print("address: %p", &A::func); 

But I want to do something like this:

A *b = new B();  printf("address: %p", &b->func);  printf("address: %p", &b->A::func); 

However this does not compile. Is it possible to do something like this, perhaps looking up the address in the vtable at runtime?

like image 230
hidayat Avatar asked Jun 18 '10 08:06

hidayat


People also ask

How do I print address from member function?

how to print the address of a member function using pointer to member function through a object of that class. class MyClass { public: void fun(void){}; }; void (MyClass::*ptr)(void); void main(void) { MyClass myObject; ptr = MyClass::fun; cout << myObject. *ptr << endl; // ... }

What is a virtual member function in C++?

A virtual function is a member function in the base class that we expect to redefine in derived classes. Basically, a virtual function is used in the base class in order to ensure that the function is overridden. This especially applies to cases where a pointer of base class points to an object of a derived class.

How do I access virtual functions?

In general, the access of the overriding member function is not known. If a virtual function is called with a pointer or reference to a class object, the type of the class object is not used to determine the access of the virtual function. Instead, the type of the pointer or reference to the class object is used.

Can a member function be virtual?

A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.


2 Answers

Currently there is no standard way of doing this in C++ although the information must be available somewhere. Otherwise, how could the program call the function? However, GCC provides an extension that allows us to retrieve the address of a virtual function:

void (A::*mfp)() = &A::func; printf("address: %p", (void*)(b->*mfp)); 

...assuming the member function has the prototype void func(). This can be pretty useful when you want to cache the address of a virtual function or use it in generated code. GCC will warn you about this construct unless you specify -Wno-pmf-conversions. It's unlikely that it works with any other compiler.

like image 186
Lukas W Avatar answered Oct 05 '22 10:10

Lukas W


Pointers to member functions are not always simple memory addresses. See the table in this article showing the sizes of member function pointers on different compilers - some go up to 20 bytes.

As the article outlines a member function pointer is actually a blob of implementation-defined data to help resolve a call through the pointer. You can store and call them OK, but if you want to print them, what do you print? Best to treat it as a sequence of bytes and get its length via sizeof.

like image 31
AshleysBrain Avatar answered Oct 05 '22 10:10

AshleysBrain