Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer to member function

Status
Not open for further replies.

aMember

Programmer
Jun 12, 2002
99
0
0
US

I need to find the address of a member function without making the function or the address identifier static.

Is this possible?
 
class xclass
{
public:
void abd(){}
};


xclass xx;

void (xclass::*a)() = &xclass::abc;
(xx.*a)();


Ion Filipski
1c.bmp
 

Thank you Ion!
Now if I wanted to print the address of the function, how would I do that?
 
you can not do that using pure C++. But you can do it using asm:

xclass xx;
long y = 123;
void (xclass::*a)() = &xclass::abc;
__asm mov eax, a
__asm mov y, eax
cout<< (long) y<< endl;

By the way, why do you need that? You will never be able to use that address even if you know it.

Ion Filipski
1c.bmp
 
Ion, how about thia (pure;) C++:
Code:
class ExpandMe {}; // Prevents some confusions...
typedef unsigned long ulong; // (or char array;)
template <class Mfp> union Trick
{
  Mfp   ptr;
  ulong img;
  Trick(Mfp p)
  { if (sizeof ptr > sizeof img) throw ExpandMe();
    img = 0; ptr = p;
  }
  operator ulong() { return img; }
}; // Now include Ion::xclass dcl...
void (xclass::*a)() = &xclass::abd; // abd (as declared;)
typedef void (xclass::*memfunptr)();
...
typedef Trick<memfunptr> Xmfp;
cout << &quot;MemFunPtr is:&quot; << hex << Xmfp(a) << endl;
What's a wonderful platform independence...
Yes, of course, it's a vain enterprise: no need to print pointers (can we imagine a debug session in batch processing mode?;)...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top