DEVELOP > c-Plus-Plus > creating a c function at compile or runtime, is this code possible?
| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"alexl" |
| Date: |
20 Apr 2005 02:13:14 AM |
| Object: |
creating a c function at compile or runtime, is this code possible? |
hi,
is this possible? should print "it worked.."
if so, what do I do in my "cplusplusbind" function?
typedef void(*ptf)();
class Hello {
void caller(){
ptf =cplusplusbind(this, m_callback);
..
call_fun_indirectly(ptf);
}
void m_callback () {
cout << "it worked!\n";
}
};
int main(int argc, char* argv[])
{
Hello h;
h.caller();
return 0;
}
ptf cplusplusbind (class T, T::*) {
return (/* do magic */ );
}
.
|
|
| User: "Karl Heinz Buchegger" |
|
| Title: Re: creating a c function at compile or runtime, is this code possible? |
20 Apr 2005 03:03:26 AM |
|
|
alexl wrote:
hi,
is this possible? should print "it worked.."
if so, what do I do in my "cplusplusbind" function?
See the FAQ
http://www.parashift.com/c++-faq-lite/pointers-to-members.html
what you want to do is not possible in the way you want it to do.
You will need 2 pointers (one for the object, the other one for
the member function in that object) to make that indirect call.
--
Karl Heinz Buchegger
kbuchegg@gascad.at
.
|
|
|
| User: "alexl" |
|
| Title: Re: creating a c function at compile or runtime, is this code possible? |
20 Apr 2005 10:24:11 PM |
|
|
Thanks for your reply. Is there any workaround? I think what I am
looking for is a "nested function" which C++ doesn't officially
support. Could my "cplusplusbind" function contain some inline assembly
language code, that would:
1. create a pointer to a function of type void fun ()
2. allocate memory for it
3. populate memory so newly created function does what I want
4. return pointer to said function
thx, -Alex
.
|
|
|
|
|

|
Related Articles |
|
|