473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 3701
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.