Question about accessing functions via pointers

J

Jeff Gaynor

Hi,

I am a moonlighting Java programmer who needs to get some JNI written in C.
Thanks to legacy considerations, it must be in C. I hope I can frame this
question in a way that you all can understand.

Now, some of the calls I make returns a pointers to an objects. The objects
themselves are written in C++. I need to call methods on these objects. How
is this done? I hope there is wither a really short answer I can be given, or
that there is a FAQ, how-to, tutorial or some such I can be pointed to.

Thanks!

Jeff
 
J

Jack Klein

Hi,

I am a moonlighting Java programmer who needs to get some JNI written in C.
Thanks to legacy considerations, it must be in C. I hope I can frame this
question in a way that you all can understand.

I can understand your question, but there is one important thing you
are misunderstanding.
Now, some of the calls I make returns a pointers to an objects. The objects
themselves are written in C++. I need to call methods on these objects. How

You are contradicting yourself here. Above you said you were talking
about C code. Suddenly you are talking about C++, an entirely
different language, which has no "methods". Actually C++ does not
have "methods" either, although it does have something called member
functions.
is this done? I hope there is wither a really short answer I can be given, or
that there is a FAQ, how-to, tutorial or some such I can be pointed to.

Thanks!

Jeff

Your biggest problem is that you are asking in the wrong place. C and
C++ are two entirely different languages, and C++ is completely
off-topic here. C++ questions go to comp.lang.c++, down the hall to
the left. But before you do that, take the link in my signature to
their FAQ. There is a section dealing with your question, but it is
not particularly simple.

>
 
P

pawanjit.s.chawla

Hi
One implementation, would be to save the function pointers for each method
, and access the method using these pointers. The function pointers can be
saved in the constructor.

pawan
 
M

Malcolm

Jeff Gaynor said:
Now, some of the calls I make returns a pointers to an objects.
The objects themselves are written in C++. I need to call methods
on these objects. How is this done?
The only sensible way is to write your code in C++ and call
ptr->method().
I hope there is wither a really short answer I can be given, or
that there is a FAQ, how-to, tutorial or some such I can be pointed > to.
You can call C from C++, but it is very difficult to call C++ from C.

If you are really determined to do it, you need to find out how classes are
implemented on the C++ compiler you are using. You will probably find that
each class has a hidden first member which indexes onto a table of function
pointers. To access from C, you need to access this hidden member and then
do lots of messy casts to retrieve the member function pointer. You then
call it, remembering to set up the "this" pointer.

The code will not be portable from one C++ compiler to the next.
 

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

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top