Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print member function address in C++

It looks like std::cout can't print member function's address, for example:

#include <iostream>

using std::cout;
using std::endl;

class TestClass
{
  void MyFunc(void);

public:
  void PrintMyFuncAddress(void);
};

void TestClass::MyFunc(void)
{
  return;
}

void TestClass::PrintMyFuncAddress(void)
{
  printf("%p\n", &TestClass::MyFunc);
  cout << &TestClass::MyFunc << endl;
}

int main(void)
{
  TestClass a;

  a.PrintMyFuncAddress();

  return EXIT_SUCCESS;
}

the result is something like this:

003111DB
1

How can I print MyFunc's address using std::cout?

like image 868
EFanZh Avatar asked Jun 20 '12 02:06

EFanZh


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; // ... }

How do you print the address of a function in C?

Without using the separate pointer for print the address of the function, You can use the name of the function in the printf . printf("The address of the function is =%p\n",test); For printing the address in the hexa-decimal format you can use the %p .

How do you find a function address?

We can get the address of a function by just writing the function's name without parentheses. Please refer function pointer in C for details. In C/C++, name of a function can be used to find address of function.

What is member function with example?

Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier. These are called friends of a class. You can declare a member function as static ; this is called a static member function.


2 Answers

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!

like image 152
templatetypedef Avatar answered Sep 27 '22 16:09

templatetypedef


I'd like to add to the other answers, that the reason that you are getting '1' printed instead of an address, is that, for some reason, the compiler is coercing your function pointer into a boolean, so that you are really calling ostream& operator<< (bool val);

This seems to be unrelated to the function being a member function.

You can uncover this kind of information with clang++ -cc1 -ast-dump:

(ImplicitCastExpr 0x3861dc0 <col:13, col:25> '_Bool' <MemberPointerToBoolean>
    (UnaryOperator 0x3861940 <col:13, col:25> 'void (class TestClass::*)(void)' prefix '&'
        (DeclRefExpr 0x38618d0 <col:14, col:25> 'void (void)' CXXMethod 0x3861500 'MyFunc' 'void (void)')))))
like image 43
Panayiotis Karabassis Avatar answered Sep 27 '22 16:09

Panayiotis Karabassis