Solved

C++ function pointers

Posted on 1999-01-22
6
16,361 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Core Java. What output will be and why ? 6 66
Java Jpanels and Jframe 8 28
JList custom Cell Renderer refresh 15 53
Java - Why doesn't this JFrame work 3 41
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
The viewer will learn how to implement Singleton Design Pattern in Java.

910 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

20 Experts available now in Live!

Get 1:1 Help Now