Solved

Can Java handle void *functionTypes as C++ does

Posted on 1997-07-16
2
211 Views
Last Modified: 2008-03-06
I am writing on a data structure code library in Java and run into the need of function types. Here is a code example in C++ ( in a binary search tree class)

this would be in the BStree.h Header file in C++

...
...

typedef void (*functionType)(Object & anItem);
// This function will be defined outside of this class

class BSTree
{
      ...
      ...

public:
      inOrderTraverse(functionType visit)
      preOrderTraverse(functionType visit)
      etc.
      etc.

} // end class


No I am wondering how to use the same mechanism in Java in order to write the same kind of function.

P.S.
As a possability I am able to transform the whole thing into an Enumeration but that doesn't seem to be a nice solution.

Thanks a lot !!
0
Comment
Question by:meggenb
[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
2 Comments
 
LVL 2

Expert Comment

by:webster030697
ID: 1223114
This doesn't answer your question exactly, but have a look at:

http://www.cerf.net/~paulp/java/btree/ 

or:

http://www.scism.sbu.ac.uk/jfl/jflcontents.html#chap15.html




0
 
LVL 1

Accepted Solution

by:
mann061997 earned 100 total points
ID: 1223115
This is the perfect application for interfaces. Define an interface containing the required function prototypes. These too can be generalized by using superclasses or interfaces.

Then, instead of passing a function pointer, pass a reference to an object implementing the interface (the interface can be used just like a type).
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

752 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