Solved

C++ function pointers

Posted on 1999-01-22
6
16,395 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java string replace 11 64
Desingning Refactoring existing code 2 32
junit as external jar or library 7 40
Java array sort 10 36
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

696 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