[java] What does a template in C++ equivalent to in Java

Started by
12 comments, last by casper 24 years, 1 month ago
quote:Original post by Jim_Ross
Javanerd''s explanation of templates, although he states that they are not templates, sounds very much like they are similar to templates. Interfaces are agreements that all subclasses shall obey a certain standard, like a template for a class. It seems like making an Object interface, then subclassing it with the types that you need and typecasting should solve all your template problems.

On a side note, I don''t see why one would "need the ''functionality''" of dynamically typed data.


Hmm? Templates are not templates eventhough they are templates? Well, I got your point though.

Templates are not like interfaces. Templates contain the IMPLEMENTATION of the class, but just leave the handled datatype to be specified at compile time.

Dynamically typed data? Templates are compile time, statically typed things. That would be handy in Java.
-Pasi Keranen
Advertisement
Oops. The reason why my post may not have seemed very coherent was because I forgot to put the not in:
"Typecasting is quite as good as templates."
I meant to say:
"Typecasting is not quite as good as templates."
That''ll teach me to not proofread my posts.
hmm, Templates are compile time?

I thought that an specialized instance of the template for each type you use it with, will be created in memory at run-time. Why would you want a version of the template for a type that youu never use it with?
[I did absolutely nothing, and it was everything that I thought it could be]
Object code is emitted for each different template type that is referenced in code. If you declare Stack<int> then a it''ll create Stack<int> object code. If you declare a Stack<char>, the compiler will create Stack<char> object code. It has to be compile time, C++ doesn''t support creation of functions as first class values. It''s possible, for example, to create run-time templated object in object extended Scheme only because it maintains lambda expressions as first class values.

This topic is closed to new replies.

Advertisement