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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I want to know address of member functions 1

Status
Not open for further replies.

JimmyK

Programmer
Sep 8, 2000
142
0
0
VN
Hi, i have this smal program



int Dummy() {}

Class Cat
{
public:
Cat(){}
int GetAge(){return MyAge;}
private:
int MyAge;
}


int main
{
int (Cat::*p1() = Cat::GetAge;
int (*p2) = Dummy;

cout << p1 << endl;
cout << p2 << endl;


}



&quot;cout << p2&quot; print the adress of Dummy function but &quot;cout << p1:&quot; doesnt.

What i want is : priting the address of GetAge function.

How can i do that?

Thanks a lot

Jimmy


mailto:nhan_tiags@yahoo.com
 
This is one idea about function's address. In this sample, you can know address of function only after calling it. The address of current function is always in the register ebp.
//--------address.cpp-----------
#include<iostream>
using namespace std;
int address;
class Cat
{
public:
Cat(){}
int GetAge()
{
_asm mov [address],ebp;
return MyAge;
}
private:
int MyAge;
};

int main()
{
Cat d;
int (Cat::* a)()=0;
cout<<address<<endl;
d.GetAge();
cout<<hex<<address<<endl;
return 0;
}
//--------address.cpp-----------
 
Thanks a lot, JolnFill
But i wonder this is the only way?

Jimmy

mailto:nhan_tiags@yahoo.com
 
Hi JolnFill,
Your post is very helpfull but you was confused that THIS pointer points to object itself, not to member functions . Here is the code for test



#include <iostream>
using namespace std;

int addr = 0;

class Cat
{
public:
Cat():MyAge(0){}
int GetAge()
{
_asm mov [addr],ECX
return MyAge;
}
private:
int MyAge;

};

int main()
{
int (Cat::*p2)()=Cat::GetAge;

Cat Cat1;
Cat *pCat=&amp;Cat1;
Cat1.GetAge();
cout << hex << addr << endl;
cout << hex << pCat << endl;
return 0;
}




Jimmy

(Helps never hurt....)




mailto:nhan_tiags@yahoo.com
 
Hi all,
I have found the way to print Cat::GetAge()
cout << hex << *(void **) &amp;p2 ;

Hope this is a useful tip.

Jimmy
mailto:nhan_tiags@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top