Solved

C++ function pointers

Posted on 1999-01-22
6
16,354 Views
Last Modified: 2011-08-18
In C++ i have a pointer to a function. I can use this pointer to point to a number of functions. How do I implement function pointers in java?
0
Comment
Question by:orchi
6 Comments
 
LVL 1

Expert Comment

by:velev
ID: 1234495
there is no way to implement this in Java
0
 

Author Comment

by:orchi
ID: 1234496
how then, do i port c++ apps that use function pointers?
0
 
LVL 1

Expert Comment

by:velev
ID: 1234497
see classes & interfaces in java.lang.reflect
/Method, Member, Field/ and invoke() method of Method class
they may help you

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 16

Accepted Solution

by:
heyhey_ earned 50 total points
ID: 1234498
there is a standard way to implement such behaviour in Java.
because Java is strictly OO language, you'll need to define Interface that describes some methods
(analog to defining a function type).

I'm posting an small example
// C++
void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *elem1, const void *elem2 ) );

// Java
interface SorterFunction
{
      public int sort(Object o1, Object o2);
}

public class qsort
{
      public static sort (Object[] objects, SorterFunction sorter)
      {
            // .... implementation
      }
}



now you can implement a StringSorter
class StringSorter implements SorterFunction
{
      public int sort(Object o1, Object o2)
      {
            /// you'll have to add a lot of more cheking here
            String st1 = (String) o1;
            String st2 = (String) o2;
            return st1.compare(st2);
      }
}


and now you can use it

String strings[] = {"zz", "aa", "bb", "cc"};
SorterFunction f = new StringSorter();
qsort.sort(srings, f);

I think that you'll get the idea.
if you have more questions - just ask.


best regards
  heyhey
0
 
LVL 8

Expert Comment

by:diakov
ID: 1234499
In general, look at velev's sggestion it is the right approach.

In Java you do not have pointers at all, but you have the introspection scheme with which you can find out about an object.

Let's suppose you have a reference to an object - r.

Class c = r.getClass() retrieves an instance to a object of type class that describes the class of the r reference.

Then you can enumerate all methods of this class by calls to c.getMethods() or just by a call to c.getMethods() if you know what you're looking for.

The Method m returned by these calls can be then called by m.invoke() with the appropriate params specified in an array.

This is the basic scenario.

Cheers,
  Nik
0
 
LVL 1

Expert Comment

by:haho
ID: 1234500
I agree with Nik.  A concrete example would be:
class A {
...
   public boolean aFunc() {
       ...
   }
}
class B {
...
   //pretendedFuncPointer is object of A that you will be using
   //to call its function.  Effectively, this works as
   //"function pointer"
   public void myFunc(A pretendedFuncPointer) {
       ...
       pretendedFuncPointer.aFunc();
       ...
   }
}

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now