Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can Java handle void *functionTypes as C++ does

Posted on 1997-07-16
2
Medium Priority
?
216 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
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 200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

971 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