472,983 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 software developers and data experts.

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 3632
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: | last post by:
Hi I have a list containing several instance address, for example: I'd like to invoke a method on each of these instance but I don't know : 1. if its possible 2. how to proceed
5
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
3
by: Dave | last post by:
Hi, Is there a general rule to use 'static' on a class member? It seems uneccessary to have to create an instance of an object just to use it's methods where declaring something as static makes...
6
by: Max | last post by:
Last time I tried to explain this on another forum it didn't go too well, so I'll try my best and if you know what I'm talking about then please tell me how to do this. I have a class, inside I...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
44
by: gregory.petrosyan | last post by:
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ????) print x All I want is to make self.data the default argument for self.f(). (I
5
by: Doru Roman | last post by:
Hi, Can somebody explain please the meaning and use of a STATIC method? Thanks, Doru
12
by: peregrine_falcon12 | last post by:
Is there a way to get a pointer to a class from inside one of the class's methods?
4
by: Paul Jansen | last post by:
Don't go away yet... this is a bit more complicated than 'how do I take a method address'! I have a class foo, and method foo::bar. I also have a tree of foo objects, which I process...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.