Hello,
is there any way to get the real address of a member function after passing it as a function argument ? (I'm almost sure the problem come from me, as I am not really fluent with pointers)
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
class TestClass
{
public:
TestClass(){};
~TestClass(){};
void func1( std::string& ){};
};
typedef void( TestClass::*TestType)( std::string& );
class Foo
{
public:
Foo(){};
~Foo(){};
template <class C, class T>
void sendMemberAddress( void(C::*funPtr)(T&), C& obj ) // without typedef
{
std::cout << "From Foo1 :: Member function address: " << (void*)&funPtr << " | Class instance address: "<< (void*)&obj << std::endl;
}
void sendMemberAddress2( TestType& funPtr, TestClass& obj ) // using typedef
{
std::cout << "From Foo2 :: Member function address: " << (void*)&funPtr << " | Class instance address: "<< (void*)&obj << std::endl;
}
};
int main()
{
Foo x;
TestClass a;
std::cout << "From Main1 :: Member function address: " << (void*)&TestClass::func1 << " | Class instance address: "<< (void*)&a << std::endl;
// Without typedef
x.sendMemberAddress( &TestClass::func1, a );
// With typedef
TestType y = &TestClass::func1;
std::cout << "From Main2 :: Member function address: " << (void*)&y << " (using TestType y = &TestClass::func1)" << std::endl;
x.sendMemberAddress2( y, a );
}
|
This will return something like :
From Main1 :: Member function address: 0x4024d4 | Class instance address: 0x22ff0f
Without typedef
From Foo1 :: Member function address: 0x22fef4 | Class instance address: 0x22ff0f
With typedef
From Main2 :: Member function address: 0x22ff04 (using TestType y = &TestClass::func1)
From Foo2 :: Member function address: 0x22ff04 | Class instance address: 0x22ff0f
|
So, as you can see the member function address (set to bold and underlined) is not the same from inside the sendMemberAddress function.
And when I try using the typedef to simplify the problem, the same problem occurs with the both prints (but the address printed from inside sendMemberAddress2 and from main for the "TestType y" is the same despite theyr are not equal to the original Member function address.)
I'm unable to get the same address after passing the pointer to the function.
But as I need the real addresses of the Member functions to verify if both pointers are pointing to the same member function I need the real address...
Further more, converting to (void*) for debug message keep returning warning about converting a member function pointer to a void*. Is there any way to do this without harassing the compiler ?
Once again, I think I am missing something with pointers by lack of knowledge.
Thank you for you help.