[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C++ function pointers

Posted on 1999-01-22
6
Medium Priority
?
16,477 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Accepted Solution

by:
heyhey_ earned 100 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month20 days, 13 hours left to enroll

864 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