how to get the address of a method in a class instance?

G

gilad

hi,
I would like to get the address of a class method in an instance.

In the following sample the third like access the method. how can I get
the address into a long variable - something like -
long addr = B->*local_method ... (this does not work)

class BCls {
public:
void method(void ) {
int k = 9;
};

BCls() { };

~BCls() {};
};

void main()
{
BCls * B = new BCls();

void (BCls::*local_method)(void) = &BCls::method;

(B->*local_method)();
}

Thanks,
Gilad.
 
V

Victor Bazarov

gilad said:
hi,
I would like to get the address of a class method in an instance.

Class method (a member function) has its address independent from
any instance. Or, most likely, I didn't understand what it is you
need.
In the following sample the third like access the method.

I am not sure I understand the preceding sentence.
how can I
get the address into a long variable - something like -
long addr = B->*local_method ... (this does not work)

You would need to use 'reinterpret_cast' to get the address of
a member function into an integral variable, *IF* there is some
integral type large enough to store the member address.
class BCls {
public:
void method(void ) {

Drop the 'void' between the parentheses. Makes your code look C.
int k = 9;
};

BCls() { };

~BCls() {};

Drop the trailing semicolons after function bodies. Without
them the code is more readable.
};

void main()

int main() // there is no 'void main' in C++
{
BCls * B = new BCls();

void (BCls::*local_method)(void) = &BCls::method;

(B->*local_method)();
}

Once you replace 'void main' with 'int main', your code becomes
valid C++.

V
 
G

gilad

Look at the three lines again -
BCls * B = new BCls(); // Instance of the class

void (BCls::*local_method)(void) = &BCls::method; // local_method is a
pointer to a function

(B->*local_method)(); // call method method in BCls in instance B

(B->*local_method) must have an address. how do I put it into a long
variable?
 
G

gilad

it was third line.

Look at the three lines again -
BCls * B = new BCls(); // Instance of the class

void (BCls::*local_method)(void) = &BCls::method; // local_method is a
pointer to a function

(B->*local_method)(); // call method method in BCls in instance B

(B->*local_method) must have an address. how do I put it into a long
variable?
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

gilad said:
I would like to get the address of a class method in an instance.

There is not such thing. Member functions belongs to the class, not to any
concrete instance. Instances of polymorphic class usually are implemented
with a pointer to a table of virtual member functions, but there are a
unique table for each class, all the instances point to it.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

gilad said:
void (BCls::*local_method)(void) = &BCls::method; // local_method is a
pointer to a function

Is not. It is a pointer to member function. Look for that concept in your
favourite C++ book or the web.
(B->*local_method) must have an address.

By order of whom? It can even not exist as indepent entity. The compiler can
emit online code able to call the member function pointed by local_method
using B as "this".

If you need such feature you can provide it by yourself: write a class that
stores a pointer to a instance and other to a member function of his class,
with an operator () that does the job.
 
M

Michiel.Salters

gilad said:
Look at the three lines again -
BCls * B = new BCls(); // Instance of the class

void (BCls::*local_method)(void) = &BCls::method; // local_method is a
pointer to a function

(B->*local_method)(); // call method method in BCls in instance B

(B->*local_method) must have an address. how do I put it into a long
variable?

Probably not. On many compilers sizeof(local_method) is a whole lot
larger
than sizeof(long). Your best bet is reinterpret_cast, but it obviously
can't do
miracles like stuffing 128 bits in 32.

Pointer-to-member-functions are not pointers-to-functions. They have to
deal
with inherited functions, virtual functions and the like.

HTH,
Michiel Salters
 
G

gilad

hi Michiel,
suppose long variable was big enough, what would be the syntax ?
128bitParam = B->*local_method .... does not compile.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top