Function member address (solved)

Hi everybody again!

Some questions I asked yesterday were interrupted. I only have some questions with boost library...

coder777 :
I still believe void* definition can be anything. So, a few questions :

- Can the boost function member variable be converted into a void* address value?

- Is the boost variable a structure or a number?

- Could you please show me an example "How to get (copy) the address of the function member, store the result into a void* then print the output function address properly?" :)
Last edited on
Perhaps you could move your programming questions to a more appropriate forum.
closed account (S6k9GNh0)
1. You can do something icky like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int test(int a, int b) {
   return a + b;
}

/* I would NEVER do this in practice */
int example(const void* blob) {
   int(**pFunc)() = (int(**)())(blob);
   return (*pFunc)();
}

int main() {
   int(*lamFunc)()  = []() -> int{ return test(1, 2); };
   std::cout << "Output: " << example(static_cast<void*>(&lamFunc)) << std::endl;
   /* I feel dirty. */
}

Of course, you can use bind to bind instances to member functions instead of using a lamda.

2. Boost Variable? I'm not sure what you mean unless you mean the variant.

3. Mission accomplished in 1.
Last edited on
computerquip wrote:
Mission accomplished in 1.
100 points!

Yeah, I see your solution, and I've just searched your new function definition :

int(*lamFunc)()

Great! It's much simpler than template. A great invention!!!!!!!
And more importantly, the variable is completely able to be converted into an address value. I'll only need to declare a void* variable, define an another function variable which must correspond with the function member's syntax, then store the member address. Finally convert it into a void*. See this :

void NUMBER::PRINT(void){printf("bla bla bla");}

Optimized solution :

1
2
3
4
5
void * function_address = 0;
void (NUMBER::*temp_address)(void) = &NUMBER::PRINT;

__asm mov eax, temp_address
__asm mov function_address, temp_address


And at least I don't have to write :

1
2
3
4
5
6
7
8
9
10
11
12
13
template <class F,void (F::*Function)()>
void * GetFunctionAddress() {
    union ADDRESS  
    {
        void (F::*func)()
        void * function_address;
    }address_data;  
 
    address_data.func = Function;  
    return address_data.function_address; 
}  
///////////////////////////////////////////////////////////
void *function_address = GetFunctionAddress<NUMBER,&NUMBER::PRINT>();


-> ???

printf("The NUMBER::PRINT address : 0x%X\n",function_address);
Last edited on
Topic archived. No new replies allowed.