[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

C++ function pointers

Posted on 1999-01-22
6
Medium Priority
?
16,456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

656 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