Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reference to a function 1

Status
Not open for further replies.

zhuzhengyu

Technical User
Jul 21, 2000
19
CN
I think java is the most excellect system I used.But I don't know if there is a reference to a function just like a pointer to a fucntion in C.If there is no,is there substitution.
 
I may be wrong, but I think the best substitution is to use polymorphism. You write an abstract class(named foo, for instance) which just declares your abstract method. Then you inherit this class and implement the abstract method in the inherited subclasses so they become concrete and can be instantiated.
Having a reference on foo(which refers in fact on an instance of a foo subclass), you can invoke the abstract method without knowing excactly the object you manipulate : you obtain the same mechanism as a function pointer, but it is more powerful.

You may also use RTTI and reflexion, but I think polymorphism is smarter.
 
Hi,
In java class contents (either variables or methods) can not be accessible out side world without class name(in case of static) or creating the instance of class. So here reference to a function is nothing else than the function name itself.
The most common use of funtion refrences(rather pointers) in c/c++ language is to make an array and use refrences at runtime to point any arbitrary function with same signature.
This runtime function selection can be done alternatively in java by polymorphism (function overridding) using interfaces or abstract classes and very useful.
 
thank you both.
your replies are very helpful.but there is also one question--if the parameter of functions are different then--if reference to a function is more flexible?

 
I would have to say that using the c way to reference to a function is less flexible because java does runtime binding. That means that the actual meaning of a class or a function is not determined until the program is actually running. You do this all the time with polymorphism. Basically you cannot reference a position to a funciton with java until runtime because a functions position in memory is not determined until runtime anyway when you make an instance of the class.....

hope this helps....

ackka
ackka@mad.scientist.com
duke_wave.gif
Java is the Future
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top