Re: How to get the absolute address of an object's member function?
On 12 Mai, 08:42, blackbiscuit <infozyzh...@gmail.com> wrote:
Suppose I have a class A which is defined as follows.
class A
{
public:
void f( int i )
{
}
};
A a;
How to get the absolute address of a.f( int )?
What exactly do you mean by absolute address of "a.f(int)"? Can you
elaborate? How would you use such an address? I'm asking because I
think what you want is this:
magic_type p = ...; // address of a.f(int)
p(23); // invoke f(int) on object a
The thing is that member functions are shared accross different
objects of the same class. You have exactly one member function 'f'
for *all* objects of class 'A'. To differentiate between objects a
member function takes an additional parameter (the 'this' pointer)
which is done automatically. What you can do is this:
void (A::*p)(int) = &A::f;
a.*p(23);
In case of data members member pointers are probably just offsets
under the hood. In case of function members member pointers are
probably the absolute address of the function. And these functions
need to know on what object they are working on (implicit 'this'
parameter) which is why you still need to say on what object you'd
like to invoke this member function.
Alternativly you can wrap such a pointer-to-member-function along with
a pointer to an object into a "function object":
auto fun2 = std::mem_fun(&A::f);
fun2(&a,23); // invokes A::f on 'a' with parameter 23
auto fun1 = std::bind1st(fun2,&a);
fun1(23); // invokes A::f on 'a' with parameter 23
Note: The use of 'auto' here for letting the compiler infer the type
is currently not supported by C++. But it's still usable if you pass
the function object to a function template like in this example:
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
int main() {
vector<string> sv;
sv.push_back("this");
sv.push_back("is");
sv.push_back("an");
sv.push_back("example");
vector<int> lv (sv.size());
transform( sv.begin(), sv.end(),
lv.begin(),
mem_fun_ref(&string::length) );
for (unsigned k=0, e=lv.size(); k<e; ++k) {
cout << lv[k] << endl;
}
}
std::transform (algorithm header) takes some iterators and a function
object to "transform" one sequence into another. In this example the
function object is a member function wrapper which accepts a reference
to a string, invokes the string::length function on it and returns the
result.
A third option is the use of a polymorphic function wrapper
(Boost.Function or tr1::function):
function<void(int)> f = bind1st(&a,mem_fun(&A::f));
f(23); // calls A::f on object 'a' with parameter 23
hope this helps,
SG