468,457 Members | 1,621 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

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

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.

Oct 11 '06 #1
8 3166
gilad wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 11 '06 #2
Victor Bazarov wrote:
gilad wrote:
>In the following sample the third like access the method.

I am not sure I understand the preceding sentence.
I think maybe s/like/line

Nate
Oct 11 '06 #3
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?


Victor Bazarov wrote:
gilad wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 11 '06 #4
it was third line.
gilad wrote:
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?


Victor Bazarov wrote:
gilad wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 11 '06 #5
gilad wrote:
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.

--
Salu2
Oct 11 '06 #6
gilad wrote:
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.

--
Salu2
Oct 11 '06 #7
gilad wrote:
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

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

Mi*************@tomtom.com wrote:
gilad wrote:
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
Oct 11 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Dave | last post: by
44 posts views Thread by gregory.petrosyan | last post: by
5 posts views Thread by Doru Roman | last post: by
12 posts views Thread by peregrine_falcon12 | last post: by
4 posts views Thread by Paul Jansen | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.